0

I have to automate a website where the user provides OTP and clicks on a button. This part cannot be automated since OTP is unknown. So i need the selenium script to wait until the user clicks on the button. Is there any way to accomplish this?(By means of selenium not with user interaction)

Tried this :

from selenium.webdriver.support.ui import WebDriverWait
WebDriverWait(driver, 1).until(lambda d: d.execute_script("return document.getElementById('loginbutton').clicked")=="true")

But the following error is thrown :

WebDriverWait(driver, 1).until(lambda d: d.execute_script("return document.getElementById('loginbutton').clicked")=="true") File "C:\Users\project\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: Process finished with exit code 1

document.getElementById('loginbutton').clicked

Gives undefined when performed on developers tool, could that be the cause for above error?

Bharath
  • 113
  • 1
  • 1
  • 10
  • .clicked is not a method or property of a dom element. – pcalkins Nov 09 '20 at 18:56
  • i got it from : https://stackoverflow.com/questions/2788191/how-to-check-whether-a-button-is-clicked-by-using-javascript – Bharath Nov 09 '20 at 18:59
  • How to check if the button is clicked or not? – Bharath Nov 09 '20 at 18:59
  • if it needs to be done manually, I'd suggest using a prompt to inform the user to do what they have to do and then click "done". You can resume your Selenium calls after the prompt is closed. – pcalkins Nov 09 '20 at 19:06
  • isn't there any other way around just by using selenium? – Bharath Nov 09 '20 at 19:11
  • You could automate webmail, get the code, enter it and click the button. – pcalkins Nov 09 '20 at 19:27
  • That's not the solution i'm looking for – Bharath Nov 09 '20 at 19:42
  • you'd need something pretty site-specific here... maybe you could: re-write parts of the DOM to intercept the form's action, or onclick event. Do something similar to a "pop-under" and then use expected condition of numberOfWindowsTobe: https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#numberOfWindowsToBe-int- (never tried this, just throwing it out there as an idea...) – pcalkins Nov 09 '20 at 20:23

1 Answers1

1

This is sort of "hacking" the method but you could just check the URL until it changes to confirm a successful login.

For example, your url would probably look something like this when trying to sign in: https://www.yoururl.com/login

Now get the url for a successful sign-in:

https://www.yoururl.com/dashboard

Now you can write a loop to grab the current URL and check it, I'll list two examples that would work below. You would need to modify it to fit your specific case.

You can use the .current_url method to help you do this.

while browser.current_url == 'https://www.yoururl.com/login':
    # still at the login page
    pass

print("Successful sign in!")

OR

while browser.current_url != 'https://www.yoururl.com/dashboard':
    # still at the login page
    pass

print("Successful sign in!")
Rohan Shah
  • 479
  • 3
  • 14