我想在附加图像中获取弹出窗口的文本。现在我在python中使用selenium来获取那些文本值。以下是我的代码 -
time.sleep(LOADING_TIME)
alert = driver.switch_to.alert
print(alert.text())
但是,当我这样做时,我得到以下错误 -
selenium.common.exceptions.NoAlertPresentException: Message: no such alert
(Session info: chrome=69.0.3497.81)
(Driver info: chromedriver=2.41.578706 (5f725d1b4f0a4acbf5259df887244095596231db),platform=Mac OS X 10.13.6 x86_64)
谁能告诉我我做错了什么?请注意:我正在使用chrome驱动程序初始化selenium驱动程序,只要您想在Chrome浏览器中添加扩展程序,就可以找到我在此处添加的示例。
问题是driver.switch_to.alert
是一个功能。
像这样改变它:
driver = webdriver.Chrome()
driver.get(your_url)
alert = driver.switch_to_alert()
alert.accept()
# Eventually do anything else.
driver.close()
[编辑]
由于selenium似乎无法识别对话框,因此您可以使用pywinauto module:来获取对话框并与之交互。
from pywinauto import Application
driver_handle = driver.current_window_handle # get driver window handle
app = Application().connect(handle = driver_handle) # initialize app
dialog = pywinauto.app.top_window_() # select the dialog
dlg.control.Addextension.Click() # click on "Add extension" button