2016-12-15 74 views
1

我试图创建一个收集一些数据的应用程序。我在Windows 10上使用了Scrapy和Selenium的Python 2.7。我以前只使用过几个网页,但是我无法选择或点击以下网站上的按钮。无法使用Python scrapy和Selenium从网页选择元素

以检查的XPath我使用了Chrome浏览器开发工具

https://aca3.accela.com/Atlanta_Ga/Default.aspx

无法点击钮标有“搜索许可证/投诉”等

这里的代码我使用:

import scrapy 
from selenium import webdriver 

class PermitsSpider(scrapy.Spider): 
    name = "atlanta" 
    url = "https://aca3.accela.com/Atlanta_Ga/Default.aspx" 

    start_urls = ['https://aca3.accela.com/Atlanta_Ga/Default.aspx',] 

    def __init__(self): 
    self.driver = webdriver.Chrome() 
    self.driver.implicitly_wait(20) 

    def parse(self, response): 
    self.driver.get(self.url) 

    time.sleep(15) 
    search_button = self.driver.find_element_by_xpath('//*[@id="ctl00_PlaceHolderMain_TabDataList_TabsDataList_ctl01_LinksDataList_ctl00_LinkItemUrl"]') 
    search_button.click() 

如果运行的代码,我得到以下错误:

NoSuchElementException异常:消息:没有这样的元件:无法找到元素:{ “方法”: “的xpath”, “选择器”: “// * @ ID =” ctl00_PlaceHolderMain_TabDataList_TabsDataList_ctl01_LinksDataList_ctl00_LinkItemUrl “]”}

我已经添加所有的睡眠和等待庄园等等,以确保在尝试选择之前页面已完全加载。我也试图选择链接文本和选择元素的其他方法。不知道为什么这个方法不适用于这个页面,它在其他人上为我工作。在运行代码时,WebDriver会在我的屏幕上打开页面,并且我看到加载的页面等。

任何帮助,将不胜感激。谢谢...

回答

0
所在的 iframe里面,所以你必须切换到该框架能够处理嵌入式元素

目标链接:

self.driver.switch_to_frame('ACAFrame') 
search_button = self.driver.find_element_by_xpath('//*[@id="ctl00_PlaceHolderMain_TabDataList_TabsDataList_ctl01_LinksDataList_ctl00_LinkItemUrl"]') 

您可能还需要与driver.switch_to_default_content()

切换回
+0

安德森是天才。谢谢,修复它。 –