Home > Enterprise >  How to interact with button based on text using Selenium and Python
How to interact with button based on text using Selenium and Python

Time:11-08

I'm trying to click buttons based on their text (using selenium). e.g., for the following HTML tag:

<span class="MuiTab-wrapper jss483">Options</span>

I've tried:

driver.find_elements_by_xpath("//*[contains(text(), 'Options')]").click()

However, Selenium can't find any element with the text: "Options".

Any ideas?

CodePudding user response:

You are using find_elements_by_xpath method. It will give you a list of web elements, not a single web element. So you can not apply .click() on the result of

driver.find_elements_by_xpath("//*[contains(text(), 'Options')]")

You should use find_element_by_xpath instead so your code will be

driver.find_element_by_xpath("//*[contains(text(), 'Options')]").click()

or clicking on the first result (or any other) from the web elements list returner, as following

driver.find_elements_by_xpath("//*[contains(text(), 'Options')]")[0].click()

Also possibly you need to add some delay / wait to let the element be loaded before accessing it.
Or maybe you need to scroll that element into the view.
Or maybe the element is inside an iframe...

CodePudding user response:

Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

xpath that you should check :

//*[contains(text(), 'Options')]

or

//span[contains(text(), 'Options')]

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

There are 4 ways to click in Selenium.

Code trial 1 :

time.sleep(5)
driver.find_element_by_xpath("//span[contains(text(), 'Options')]").click()

Code trial 2 :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(), 'Options')]"))).click()

Code trial 3 :

time.sleep(5)
button = driver.find_element_by_xpath("//span[contains(text(), 'Options')]")
driver.execute_script("arguments[0].click();", button)

Code trial 4 :

time.sleep(5)
button = driver.find_element_by_xpath("//span[contains(text(), 'Options')]")
ActionChains(driver).move_to_element(button).click().perform()

Imports:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

Also, find_elements returns a list, change that to find_element you should be good to go ahead.

or else use list index to point web element.

elems = driver.find_elements_by_xpath("//*[contains(text(), 'Options')]")
elems[0].click()

This is not recommended way.

CodePudding user response:

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

  • Using xpath:

    driver.find_element(By.XPATH, "//span[contains(@class, 'MuiTab-wrapper') and contains(., 'Options')]").click()
    

The desired element is a JavaScript enabled 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 XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(@class, 'MuiTab-wrapper') and contains(., 'Options')]"))).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
    
  • Related