我正在 python 中使用 selenium webdriver 来抓取弹出窗口的内容。我用过driver.switch_to_alert.text()
检索内容。
当我调用 driver.switch_to_alert
时,我收到警报对象但我无法使用acccept()
, dismiss()
和对象的文本功能。
如果我调用警报,我就会得到该对象
<selenium.webdriver.common.alert.Alert at 0x438fbe0>
但是如果调用alert.text,我会收到以下错误
NoAlertPresentException
Traceback (most recent call last)
<ipython-input-162-7b8c4cd45721> in <module>()
----> 1 alert.text
C:\Users\\Anaconda\lib\site-packages\selenium\webdriver\common\alert.pyc in text(self)
63 Gets the text of the Alert.
64 """
---> 65 return self.driver.execute(Command.GET_ALERT_TEXT)["value"]
66
67 def dismiss(self):
我尝试将执行延迟sleep(5)
但没有任何效果。
最佳答案
Wait for the alert to be present然后切换到它:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
wait = WebDriverWait(driver, 10)
wait.until(EC.alert_is_present())
alert = driver.switch_to.alert
alert.accept()
关于python - switch_to_alert.text() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33466853/