繁体   English   中英

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

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

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

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

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

我使用了Chrome开发工具来检查XPath等。

这是我正在使用的代码:

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:消息:没有这样的元素:无法定位元素:{“method”:“xpath”,“selector”:“// * @ id =”ctl00_PlaceHolderMain_TabDataList_TabsDataList_ctl01_LinksDataList_ctl00_LinkItemUrl“]”}

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

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

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

暂无
暂无

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

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