简体   繁体   English

如何使用python在Selenium Web驱动程序中等待用户单击按钮?

[英]How to wait for a button to be clicked by user in Selenium web-driver using python?

I've a Login form like below.我有一个如下所示的登录表单。

在此处输入图片说明

Textbox for username, password, captcha and SIGN-IN button are clickable and visible from beginning .用户名、密码、验证码和登录按钮的文本框从一开始就可点击和可见 Using Selenium I can provide input for username & password.使用 Selenium 我可以提供用户名和密码的输入。 Then, I've to wait for entering the CAPTCHA by users, and then click on SIGN IN button by user again.然后,我必须等待用户输入验证码,然后再次单击用户登录按钮。

After clicking the SIGN-IN button webdriver should take the control for next.单击登录按钮后,webdriver 应控制下一步。

So, webdriver should wait until the SIGN-IN button is clicked (for user1, it may take 2 seconds to enter CAPTCHA, but for user2 it may take 5 seconds to enter the CAPTCHA).所以,webdriver应该等到SIGN-IN按钮被点击(对于user1,可能需要2秒进入CAPTCHA,但对于user2,可能需要5秒进入CAPTCHA)。

This is the HTML for SIGN IN button.这是登录按钮的 HTML。

<button _ngcontent-c4="" class="search_btn" type="submit">SIGN IN</button>

I tried with below, But, it's not working.我在下面尝试过,但是,它不起作用。

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("url")
btnSignIn = driver.find_element_by_xpath("//form/button[@type='submit' and @class='search_btn']")
WebDriverWait(driver, timeout=600).until(EC.staleness_of(btnSignIn))

How can I do that ?我怎样才能做到这一点 ? Thanks in advance.提前致谢。

You can implement below solution:您可以实施以下解决方案:

driver = webdriver.Chrome()
driver.get("url")

# Code for entering Username, Password
...
# Wait until user enter Captcha
input("Press ENTER after filling CAPTCHA")
driver.find_element_by_xpath("//form/button[@type='submit' and @class='search_btn']").click()

This should allow to wait until user press ENTER key and then execute Submit button click这应该允许等到用户按下 ENTER 键,然后执行提交按钮单击

If you don't want user to interact with browser, but with console only, you can improve code as below:如果您不希望用户与浏览器交互,而只与控制台交互,您可以改进代码如下:

driver = webdriver.Chrome()
driver.get("url")

# Code for entering Username, Password
...
# Wait until user enter Captcha (in console) and press ENTER 
captcha_input = driver.find_element_by_xpath('//input[@placeholder="Enter Captcha"]')
captcha = input("Enter CAPTCHA and Press ENTER\n")
captcha_input.send_keys(captcha)
driver.find_element_by_xpath("//form/button[@type='submit' and @class='search_btn']").click()

Similar to Andersson's answer, which didn't work for me because the driver complained it couldn't find the element (my guess is the driver is stateful so after logging in the driver didn't have the new page's data)类似于 Andersson 的回答,这对我不起作用,因为驱动程序抱怨它找不到元素(我的猜测是驱动程序是有状态的,因此登录驱动程序后没有新页面的数据)

driver = webdriver.Chrome()
driver.get("url")

# Code for entering Username, Password
...
# Wait until user enter Captcha (in console) and press ENTER 
input("Login manually and press ENTER\n")
driver.get("logged in url") # cookies should keep you logged in
driver.find_element_by_xpath("blah")

But I found what worked for me is adding a line to reload the url.但我发现对我有用的是添加一行来重新加载 url。 This will reload the site but thanks to cookie the driver is now logged in. Hope this helped if his previous answer didn't :)这将重新加载站点,但多亏了 cookie,驱动程序现在已登录。如果他之前的回答没有,希望这会有所帮助:)

你可以试试这个

WebDriverWait(driver, 10).until(EC.element_to_be_clickable(btnSignIn))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Selenium Web驱动程序python单击网页中目录列表的不同链接 - how to click on different links of directory listing in a web page using selenium web-driver python 更改 Selenium 网络驱动程序的用户代理 - Change user-agent for Selenium web-driver 我如何在Selenium Web驱动程序python中使用多线程进行Web爬网 - how can i use multi-threading for web-crawling in selenium web-driver python 无法使用Selenium Web驱动程序python从自动完成下拉列表中选择多个选项 - Unable to select multiple options from autocomplete drop-down using selenium web-driver python Selenium Web驱动程序自动化无法正常工作 - Selenium Web-driver automation not working properly 我如何使用Selenium Web驱动程序以正常方式打开Firefox(没有“地址无效”屏幕) - How do I Open Firefox in a normal way(with out “The address isn't valid” screen) using selenium web-driver 将Selenium Web驱动程序中的两个字符串与Python中的行为驱动开发进行匹配 - Match two strings in Selenium web-driver with behavior driven development in Python Python selenium web-driver:从下拉列表中选择值(无此类元素异常) - Python selenium web-driver: selecting value from the dropdown list (No such Element Exception) 在 for 循环中运行 3 个变量 python selenium 网络驱动程序以打印包括链接在内的所有数据 - run 3 variables in for loop python selenium web-driver to print all data including links Selenium Web-Driver Firefox Profile - 禁用弹出窗口和警报窗口 - Selenium Web-Driver Firefox Profile - Disable popup and alert windows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM