1

Im not sure if that was quite the best title but Im not sure how else to describe it, I am trying to use selenium to automate 2fa on a website so all I need to do is anwser the phone call and the script will take care of the rest however the button I am trying to get selenium to click keeps showing up as unable to locate even though its always in the same place and never changes here is my code in python

callMe = driver.find_element('xpath', '//*[@id="auth_methods"]/fieldset/div[2]/button')
callMe.click()
sleep(25)

this is one of three buttons that all have the same element information besides the xpaths here are all 3 button elements I am trying to grab the second one

<button tabindex="2" type="submit" class="positive auth-button"><!-- -->Send Me a Push </button>
<button tabindex="2" type="submit" class="positive auth-button"><!-- -->Call Me </button>
<button tabindex="2" type="submit" class="positive auth-button"><!-- -->Text Me </button>

I am not sure how else I can locate the second button besides using the xpath but that is not working and I dont know if I can or how I can search for the button based off the text that is inside it.

kidlo22
  • 7
  • 2

4 Answers4

0

Did you try with By ?

from selenium.webdriver.common.by import By

callMe = driver.find_element(By.XPATH, '//*[@id="auth_methods"]/fieldset/div[2]/button')
Bruno93600
  • 53
  • 8
0

Try using By.cssselector, get the css selector of the main body html for the button you want.

callMe = driver.find_element(By.css_selector, 'selectorofbodyhtml')
callme.click()
Deepak
  • 57
  • 1
  • 8
0

Try below xpath

//button[starts-with(text(),'Call Me')]
Ketan Pardeshi
  • 516
  • 1
  • 5
  • 8
0

The desired element Call Me is a dynamic element, so to click on it you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using XPATH and contains():

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'Call Me')]"))).click()
    
  • Using XPATH and starts-with():

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[starts-with(., 'Call Me')]"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352