gpt4 book ai didi

python - 如何在 selenium headless 浏览器中退出模态对话框窗口?

转载 作者:太空宇宙 更新时间:2023-11-03 11:34:13 29 4
gpt4 key购买 nike

我正在为我的项目使用 selenium2 + python + amazon ec2 (Ubuntu 11.04)。

由于亚马逊 ec2 中的 Headless 原因,我正在使用 PyVirtualDisplay由于服务器中没有显示。

当我运行我的项目时,出现异常

Traceback (most recent call last):
File "spyfu_ad_crawler_server.py", line 68, in <module>
main(i[0])
File "spyfu_ad_crawler_server.py", line 34, in main
WebDriverWait(browser, 10).until(lambda driver : driver.find_element_by_xpath("/html/body/form/div[3]/div[3]/div/div/table/tbody/tr[3]/td/tab le/tbody/tr[2]/td/table/tbody"))
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/support/wait.py", line 49, in until
value = method(self._driver)
File "spyfu_ad_crawler_server.py", line 34, in <lambda>
WebDriverWait(browser, 10).until(lambda driver : driver.find_element_by_xpath("/html/body/form/div[3]/div[3]/div/div/table/tbody/tr[3]/td/table/tbody/tr[2]/td/table/tbody"))
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 210, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 645, in find_element
{'using': by, 'value': value})['value']
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 153, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 147, in check_response
raise exception_class(message, screen, stacktrace)
WebDriverException: Message: u'Modal dialog present'


Traceback (most recent call last):
File "spyfu_ad_crawler_server.py", line 75, in <module>
browser.get(base_url)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 165, in get
self.execute(Command.GET, {'url': url})
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 153, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 147, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: u'Modal dialog present'

应该怎么解决?

在selenium中如何在python中处理这样的对话框?

最佳答案

请在执行 driver.get() 之前和之后运行以下代码:

 driver.execute_script('window.onbeforeunload = function() {}')

它应该可以工作。我没有检查过,但 Firefox 网站应该有关于此事件的更多详细信息。

基本上,您是在网页中重置提供该模式对话框的处理程序。所以你不会看到它显示出来。这种方法几乎解决了我的问题。

关于python - 如何在 selenium headless 浏览器中退出模态对话框窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9195753/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com