简体   繁体   English

无法使用Python scrapy和Selenium从javascript网页中选择元素

[英]Cannot select element from a javascript webpage using Python scrapy and Selenium

I'm attempting to create an app that gathers some data. 我正在尝试创建一个收集一些数据的应用程序。 I'm using Python 2.7 with Scrapy and Selenium on Windows 10. I've done this with only a few web pages previously, however, I'm not able to select or click on a button from the following website. 我在Windows 10上使用带有Scrapy和Selenium的Python 2.7。之前我只使用了几个网页,但是,我无法从以下网站中选择或点击按钮。

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

Not able to click on the botton labeled "Search Permits/Complaints" 无法点击标有“搜索许可/投诉”的按钮

I've used the Chrome dev tools in order to inspect the XPaths, etc. 我使用了Chrome开发工具来检查XPath等。

Here's the code that I'm using: 这是我正在使用的代码:

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()

When Running that code, I get the following error: 运行该代码时,我收到以下错误:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*@id="ctl00_PlaceHolderMain_TabDataList_TabsDataList_ctl01_LinksDataList_ctl00_LinkItemUrl"]"} NoSuchElementException:消息:没有这样的元素:无法定位元素:{“method”:“xpath”,“selector”:“// * @ id =”ctl00_PlaceHolderMain_TabDataList_TabsDataList_ctl01_LinksDataList_ctl00_LinkItemUrl“]”}

I've added all manor of sleeps and waits, etc in order to ensure that the page is fully loaded before attempting the selection. 我已经添加了所有睡眠和等待的庄园,以确保在尝试选择之前页面已满载。 I've also attempted to select the Link Text, and other methods for selecting the element. 我还试图选择链接文本和其他方法来选择元素。 Not sure why this method isn't working on this page where it works for me on others. 不知道为什么这个方法不能在这个页面上工作,它对我有用。 While running the code, the WebDriver does open the page on my screen, and I see the page loaded, etc. 在运行代码时,WebDriver会在我的屏幕上打开页面,我看到页面已加载等。

Any help would be appreciated. 任何帮助,将不胜感激。 Thank you... 谢谢...

Target link located inside an iframe , so you have to switch to that frame to be able to handle embedded elements: 位于iframe内的目标链接,因此您必须切换到该帧才能处理嵌入的元素:

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

You also might need to switch back with driver.switch_to_default_content() 您还可能需要切换回driver.switch_to_default_content()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM