如何在selenium无头浏览器中退出模式对话框窗口?

发布于 2025-01-03 06:20:55 字数 2527 浏览 2 评论 0原文

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

由于亚马逊 ec2 中的无头原因,我使用 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' 

它应该如何解决?

如何在Python中的selenium中处理此类对话框?

I am using selenium2 + python + amazon ec2 (Ubuntu 11.04) for my project.

For Headless reason in amazon ec2, I am using PyVirtualDisplay Since there is no display in server.

when i am running my project, I am getting the exception

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' 

How it is supposed to solve?

How to handle such dialog boxes in selenium in python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

网白 2025-01-10 06:20:55

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

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

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

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

Please run the following code before and after doing driver.get():

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

It should work. I haven't checked but Firefox website should have more details on this event.

Basically you are resetting the handler in the webpage that gives that modal dialog box. So you won't see it displayed. This approach pretty much solved my issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文