python - switch_to_alert.text() 不起作用

标签 python selenium

我正在 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/

相关文章:

python - sqlite3 使用 python 和 python cgi 插入

python - 在 For 循环中迭代两个范围

Python:threading.timer 不遵守间隔

java - 如何通过Selenium阻止chrome中的弹出窗口

selenium - 操作类 - 单击(WebElement ele)函数不单击

Selenium 网络驱动程序 : Random Error determining if element is visible

python - glob 生成器的类型注释

python - 凯拉斯图像数据生成器 : why are the outputs of my CNN reversed?

java - 单击菜单链接时获取元素不可单击异常

java - 如何使用selenium java向左和向右移动 slider