SHM
SHM

Reputation: 303

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

I've a Login form like below.

enter image description here

Textbox for username, password, captcha and SIGN-IN button are clickable and visible from beginning. Using Selenium I can provide input for username & password. 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.

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).

This is the HTML for SIGN IN button.

<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.

Upvotes: 4

Views: 4331

Answers (3)

Chien Hao Tan
Chien Hao Tan

Reputation: 1

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)

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. This will reload the site but thanks to cookie the driver is now logged in. Hope this helped if his previous answer didn't :)

Upvotes: 0

Andersson
Andersson

Reputation: 52695

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

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()

Upvotes: 2

Ishita Shah
Ishita Shah

Reputation: 4035

You may try this,

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

Upvotes: -1

Related Questions