今天我无法在此处获取代码00 01可以通过Selenium WebDriver从Python获取javascript alert()框。
关于此主题的我google search给了我许多结果,例如this one使用EC.alert_is_present()
和driver.switch_to_alert()
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("...some page...")
WebDriverWait(driver, 5).until(EC.alert_is_present())
driver.switch_to_alert().accept()
我遵循这一点并在我的github上编写一个简单的代码00 01试图从Python代码访问警报对象。虽然我失败了超时异常,如下所示,即webdriver无法获取alert()框对象。
Traceback (most recent call last):
File "util/sandbox/my_sandbox/test_alert_box.py", line 49, in <module>
WebDriverWait(driver,10).until(EC.alert_is_present()) #TODO Why alert box not available? We got error here
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
因此,如果您知道如何使其发挥作用,请分享。谢谢!