1

I want to fetch the following web element using selenium and python

<td class="letterbreak first">default</td>

And I want to find it based on the inner HTML text default. I can't use the class because there are other elements using the same class. I have tried:

driver.find_element(By.XPATH,"//*[contains(@innerHTML,'default')]")

But I get a NoSuchElementException.

Is it possible to find based on innerHTML? If so, how can I do it?

2
  • text() instead of @innerHTML should work. Commented Dec 15, 2020 at 20:47
  • @arundeepchohan that did not work but driver.find_element(By.XPATH,"//*[text()='default']") did. Thanks. Commented Dec 15, 2020 at 22:10

1 Answer 1

2

Use either of the xapth.

driver.find_element(By.XPATH,"//*[contains(text(),'default')]")

OR

driver.find_element(By.XPATH,"//*[text()='default']")

To avoid synchronization issue induce WebDriverWait() and wait for visibility_of_element_located() and following xpath

WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[contains(text(),'default')]")))

You need to import below libraries.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Sign up to request clarification or add additional context in comments.

1 Comment

The second format worked but the first did not. Does the first do a partial match and the second an exact match?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.