如何接受由“获取”提醒的警报在硒(蟒蛇,chromedriver)?

时间:2016-10-26 10:21:23

标签: python selenium selenium-chromedriver

我正在尝试使用selenium从某个页面导航到另一个页面:

driver = webdriver.Chrome()
driver.get("...some page...")
...  # the alert does not exist yet and thus cannot be accepted
driver.get("...some other page...") # the alert pops up here and blocks navigation to 'some other page'
# execution never reaches here
...

现在,离开某个页面'触发警报,要求确认确实要离开页面。这会永远阻止执行。设置了隐式超时,但未由此触发。我不能让selenium接受警报,因为只有在调用' get'后才会出现。

有什么方法吗?

非常感谢!

2 个答案:

答案 0 :(得分:1)

尝试以下代码并告诉我它是否无效

// e.g., on a view controller’s viewDidAppear() method:
if let styleMask = view.window?.styleMask
{
    view.window!.styleMask = NSWindowStyleMask(rawValue: styleMask.rawValue | NSWindowStyleMask.resizable.rawValue)
}

答案 1 :(得分:0)

尝试overwriting javascript alert功能。

driver.get('https://stackoverflow.com/questions/40259660/how-to-accept-alert-that-is-triggered-by-get-in-selenium-python-chromedriver#40259660')
wmd_input = ref_settings.driver.find_element(By.ID, 'wmd-input')
wmd_input.click()
wmd_input.send_keys('That's a test for a model JS dialog')

#the following line overwrites the alert function
driver.execute_script("""window.alert = function() {};alert = function() {};""")
time.sleep(3)
ref_settings.driver.get('http://stackoverflow.com')