【发布时间】:2017-12-19 06:35:09
【问题描述】:
我有一组 Web 抓取工具,旨在使用 Selenium ChromeDriver 在 Python 3.6 中运行。它们都运行得非常完美。
本周我将 Selenium 更新到 v2.8,将 ChromeDriver 更新到 v2.34。
立即,刮板无法正常工作并在爬行的早期崩溃。
我有一个sys.stdout 的小实现,它同时输出到 .txt 和控制台,所以我开始注意到错误是这样的:
Message: no such frame
(Session info: chrome=63.0.3239.108)
(Driver info: chromedriver=2.34.522940
(1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 10.0.15063 x86_64)
或
Message: no such element: Unable to locate element:
{"method":"name","selector":"txtClave"}
(Session info: chrome=63.0.3239.108)
(Driver info: chromedriver=2.34.522940
(1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 10.0.15063 x86_64)
Message: no such element: Unable to locate element:
{"method":"xpath","selector":"//*[@id="ctl00_cp_wz_ddlTarjetas"]/option[2]"}
(Session info: chrome=63.0.3239.108)
(Driver info: chromedriver=2.34.522940
(1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 10.0.15063 x86_64)
Message: no such element: Unable to locate element:
{"method":"xpath","selector":"//*[@id="ctl00_cp_wz_ddlTarjetas"]/option[3]"}
(Session info: chrome=63.0.3239.108)
(Driver info: chromedriver=2.34.522940
(1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 10.0.15063 x86_64)
Message: no such element: Unable to locate element:
{"method":"xpath","selector":"//*[@id="ctl00_cp_wz_ddlTarjetas"]/option[4]"}
(Session info: chrome=63.0.3239.108)
(Driver info: chromedriver=2.34.522940
(1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 10.0.15063 x86_64)
之后通常会出现以前从未出现过的 ChromeDriver 崩溃 Windows 消息:chromedriver.exe has stopped working。
在查看 Chrome 窗口时,通过调试,我怀疑错误是在应该让蜘蛛等待页面加载的行引起的,但它没有等待,所以它无法找到元素。
导致错误的行示例:
self.driver.find_element_by_name('txtUsuario').send_keys(user + Keys.RETURN)
self.driver.find_element_by_name('txtClave').send_keys(passwd + Keys.RETURN)
...
self.driver.switch_to.default_content()
self.driver.switch_to_frame('Fmenu')
self.driver.find_element_by_xpath(XPATH_POSICIONGLOBAL).click()
发现它太难过(基本上是投降)而无法通过向每个要交互的元素添加显式等待来故障转移(可能是因为我有超过一百个?)。
我希望有人能帮助我找出可能导致整个工作蜘蛛无法在这些新版本的 ChromeDriver / Selenium 中爬行的原因,并解决此问题的代码优雅且易于实施的解决方案。
例如,我尝试将 implicitly_wait 附加到 WebDriver 会话,但它根本不起作用。
def __init__(self):
self.driver = webdriver.Chrome(PATH_WEBDRIVER)
self.driver.implicitly_wait(10)
最后,我使用 IDLE 一次运行了两个失败的蜘蛛 1 行,它可以工作!那么......为什么它不能在常规的 Spider 执行中工作?????
非常感谢提前
【问题讨论】:
-
将等待时间增加到100
-
您的 Chrome 是什么版本?
-
我使用的是最新的 Chrome 版本。此外,我怀疑implicitly_wait 失败了,因为它在 10 秒或 100 秒内根本什么都不做。我现在不在家,我会在一段时间内发布它给出的错误
标签: python selenium web-crawler selenium-chromedriver