[英]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.