简体   繁体   English

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

[英]How to quit modal dialog window in selenium headless browser?

I am using selenium2 + python + amazon ec2 (Ubuntu 11.04) for my project. 我正在为项目使用selenium2 + python +亚马逊ec2(Ubuntu 11.04)。

For Headless reason in amazon ec2, I am using PyVirtualDisplay Since there is no display in server. 由于Amazon ec2中没有Head的原因,我正在使用PyVirtualDisplay,因为服务器中没有显示。

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? 如何在python中的硒中处理此类对话框?

Please run the following code before and after doing driver.get(): 请在执行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. 我尚未检查,但Firefox网站应该对此事件有更多详细信息。

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. 这种方法几乎解决了我的问题。

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

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