How to exit a modal dialog box in a seleted browser without a browser?

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

For a carefree reason in amazon ec2 I use PyVirtualDisplay since there is no display on the server.

when i run my project i get an 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 should this be decided?

How to handle such dialogs in selenium in python?

+4
source share
1 answer

Run the following code before and after driver.get () is executed:

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

It should work. I have not tested, but the Firefox website should have more details about this event.

Basically, you reinstall the handler on the web page that gives this modal dialog box. Therefore, you will not see it. This approach pretty much solved my problem.

+2
source

Source: https://habr.com/ru/post/1395424/


All Articles