1

Trying to make my bot click on a submit button.

<div class="usertext-buttons">
<button type="submit" onclick="" class="save">save</button>
<button type="button" onclick="return cancel_usertext(this);" class="cancel" style="display:none">cancel</button>
<span class="status"></span></div>

I want to get the second row element with the type="submit"

driver.find_element_by_xpath doesn't work since the xpath is different for every post. What can I pull here that generally works?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Blue
  • 51
  • 4

2 Answers2

2

To click on the element with text as save you can use either of the following Locator Strategies:

  • Using css_selector:

    driver.find_element_by_css_selector("button.save[type='submit'][onclick]").click()
    
  • Using xpath:

    driver.find_element_by_xpath("//button[@class='save' and text()='save'][@type='submit' and @onclick]").click()
    

Ideally, to click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.save[type='submit'][onclick]"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='save' and text()='save'][@type='submit' and @onclick]"))).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
1

Try using css selector:

driver.find_element_by_css_selector('div.usertext-buttons > button[type=submit]').click()
frianH
  • 7,295
  • 6
  • 20
  • 45