admin管理员组文章数量:1208155
I am running automated (python) selenium tests on a chrome browser, and sometimes when a page is reloaded a popup appears on the screeen:
Is it possible to configure the chrome selenium browser in such a way, that this popup does not appear? If so, how to do that? Or are there other ways to supress this popup? Or to accept it?
I am running automated (python) selenium tests on a chrome browser, and sometimes when a page is reloaded a popup appears on the screeen:
Is it possible to configure the chrome selenium browser in such a way, that this popup does not appear? If so, how to do that? Or are there other ways to supress this popup? Or to accept it?
Share Improve this question edited Apr 9, 2019 at 11:47 undetected Selenium 193k44 gold badges303 silver badges378 bronze badges asked Apr 9, 2019 at 7:12 AlexAlex 44.3k100 gold badges298 silver badges513 bronze badges3 Answers
Reset to default 7This popup with text as Reload site? Changes you made may not be saved is the implementation of onbeforeunload
property of WindowEventHandlers
onbeforeunload
The onbeforeunload property of the WindowEventHandlers mixin is the EventHandler for processing beforeunload events. These events fire when a window is about to unload its resources. At this point, the document is still visible and the event is still cancelable.
Solution
There are different strategies available to handle this popup.
Chrome solution: Using
--disable-popup-blocking
through ChromeOptions():from selenium import webdriver options.add_argument("--disable-popup-blocking") driver=webdriver.Chrome(chrome_options=options, executable_path=/path/to/chromedriver')
Firefox solution: Using
dom.disable_beforeunload
through FirefoxProfile():from selenium import webdriver profile = webdriver.FirefoxProfile() profile.set_preference("dom.disable_beforeunload", True) driver = webdriver.Firefox(firefox_profile = profile)
Cross Browser solution: As a Cross Browser solution, you can disable this dialog invoking the
executeScript()
to set window.onbeforeunload asfunction() {};
and you can use the following solution:driver.execute_script("window.onbeforeunload = function() {};")
JQuery based solution:
$I->executeJS( "window.onbeforeunload = null" );
You can find a relevant discussion in How to handle below Internet Explorer popup “Are you sure you want to leave this page?” through Selenium
I know this doesn't answer the suppression part of the question, but..
Try this piece of code for accepting the popup:
driver.SwitchTo().Alert().Accept();
For java, you can usee the following code,
// Set the path to the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path_to_chromedriver");
// Create ChromeOptions instance
ChromeOptions options = new ChromeOptions();
// Disable pop-ups
options.addArguments("--disable-popup-blocking");
// Create WebDriver instance
WebDriver driver = new ChromeDriver(options);
// Rest of your code...
Hope this helps. Thanks.
本文标签:
版权声明:本文标题:javascript - How to disable a "Reload site? Changes you made may not be saved" popup for (python) selenium tes 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738695448a2107349.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论