1

On a webpage, I have a button element with various attributes.

<button class="alert-area rj-button--plain ng-binding" ng-click="forceStructSync()" bs-tooltip="" data-original-title="This runs a structure sync, and makes sure our warehouse is aware of any new tables you've added to your database recently." data-placement="right" button-text="Check for new tables and columns"> <span class="rj-icon--refresh"></span> Check for new tables and columns </button>

I have tried with selenium with different forms of syntax to select and click the element using its button-text attribute, but am unsure where the error is coming from. It is definitely not a timing issue, in other words, the page is loaded when the find command is executed.

from selenium import webdriver
driver = webdriver.Chrome()
import time


driver.get ('https://mywebsite.com')
#time.sleep(10)
driver.find_element_by_css_selector(".button[button-text='Check for new tables and columns']").click()

I have also tried the camel case buttonText and button_text in place button-text, but this throws the same error.

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to lo
cate element: {"method":"css selector","selector":".button[button-text='Check for new tabl
es and columns']"}

Any suggestions on what might be the correct syntax here?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
iskandarblue
  • 7,208
  • 15
  • 60
  • 130

2 Answers2

0

Actually the answer was not to include the period before button so this worked:

driver.find_element_by_css_selector("button[button-text='Check for new tables and columns']").click()
iskandarblue
  • 7,208
  • 15
  • 60
  • 130
  • 1
    The `.` indicates a class. In this case, BUTTON is a tag and not a class. That's why removing the `.` fixed this locator. – JeffC Feb 16 '21 at 04:17
0

To click on the element you can use either of the following Locator Strategies:

  • Using css_selector:

    driver.find_element(By.CSS_SELECTOR, "button.alert-area.rj-button--plain.ng-binding[ng-click^='forceStructSync'][data-original-title^='This runs a structure sync']").click()
    
  • Using xpath:

    driver.find_element(By.XPATH, "//button[@class='alert-area rj-button--plain ng-binding' and starts-with(@ng-click, 'forceStructSync')][contains(., 'Check for new tables and columns')]").click()
    

The desired element is a dynamic element, so ideally, to click on the element you need 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.alert-area.rj-button--plain.ng-binding[ng-click^='forceStructSync'][data-original-title^='This runs a structure sync']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='alert-area rj-button--plain ng-binding' and starts-with(@ng-click, 'forceStructSync')][contains(., 'Check for new tables and columns')]"))).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