【发布时间】:2019-01-11 20:35:15
【问题描述】:
上下文:
- 我的脚本使用 selenium webdriver 启动到网站
- 用户在网站上填写了一些东西
- 用户将单击一个按钮,该按钮将弹出一个
confirm()dialog box,询问用户“您要提交数据吗”
我的意图:
我的脚本会等到用户点击按钮。一旦它检测到用户点击了按钮,我的脚本就会得到一个元素的值,然后(不知何故)点击dialog box上的OK。
问题:
如何等待用户点击按钮?
然后如何在dialog box 上单击“确定”?
附加说明:
使用:chromedriver,Python 2.7
按钮:<input id="submitID" type="button" class="medium" value="Submit filled Data">
[编辑] 一些代码 sn-p:
对话框弹出是javascript popup:
if (window.confirm("Are you sure you want to submit the data?")) {
this.SaveData();
}
我的代码(针对这个问题进行了简化和修改):
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
PATH_TO_CHROMEDRIVER = 'path/to/chromedriver'
URL = 'https://website-asking-user-to-fill-in-stuff.com'
driver = webdriver.Chrome(PATH_TO_CHROMEDRIVER)
driver.get(URL)
while True:
# loop until user close the chrome.
# If anyone has any better idea to
# WAIT TILL USER CLOSE THE WEBDRIVER, PLEASE SHARE IT HERE
try:
# --- main part of the code ---
waitForUserToClickButton() # this is what I need help with
driver.find_element_by_id('elementID').text
confirmJavascriptPopup() # this is also what I need help with
except WebDriverException:
print 'User closed the browser'
exit()
【问题讨论】:
-
是 javascript 还是 html 弹出窗口?此外,您应该分享一些代码 sn-p 来了解您到目前为止尝试过的内容,并分享 html 来获取警报(如果它是 html 弹出窗口)
-
这是一个非常有趣的问题,但我认为通过 Selenium 是不可能的。
-
@theGuy 这是一个 javascript 弹出窗口。
标签: python selenium button selenium-webdriver click