我正在尝试使用 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”后出现。
有什么办法可以解决这个问题吗?
非常感谢!
最佳答案
尝试以下代码,如果不起作用请告诉我
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
driver = webdriver.Chrome()
driver.get("...some page...")
try:
WebDriverWait(driver, 5).until(EC.alert_is_present())
driver.switch_to_alert().accept()
except TimeoutException:
print("Alert not found. Move on...)
driver.get("...some other page...")
关于python - 如何在selenium(python,chromedriver)中接受由 'get'触发的警报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40259660/