-1

So I'm trying to click a link if it contains this string somewhere in its html. Here is the HTML:

        <span class="_3A9bf_kZ6VBA2VBRND5gvf">r/smashbros</span>

I'm trying to click the link if it contains the word "smash" somewhere in the element, how would I go about this? I know I can easily do

driver.find_element_by_class_name("_3A9bf_kZ6VBA2VBRND5gvf").click()

I'm trying to find a way more dynamic way to find the element, since there is a chance that the element might not be there. Any help is appreciated.

1 Answer 1

-1

Take a look at xpath's contains method: https://www.guru99.com/using-contains-sbiling-ancestor-to-find-element-in-selenium.html#1

try:
    driver.find_element_by_xpath("//span[contains(text(),'smash')]")
except NoSuchElementException as e:
    print('No Such Element found')
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.