0

Lets say I have html code with three links

<a href="hey123"> Whatever </a>
<a href="hey321"> Whatever </a>
<a href="hi123"> Whatever </a>

and I want to use selenium to find all elements which have a href tag which includes the string "hey" (in this case the first two links). How would I write python Selenium code which accomplishes this?

2 Answers 2

2

This works:

all_href = driver.find_elements(By.XPATH,  "//*[contains(@href, 'hey')]")
print(len(all_href)
Sign up to request clarification or add additional context in comments.

Comments

1

This XPath will do this work:

"//a[contains(@href,'hey')]"

To use that with Selenium you will need a find_elements or findElements method, depends on the language binding you use with Selenium.
For Selenium in Python this will give you the list of all such elements:

all_hey_elements = driver.find_elements(By.XPATH,  "//a[contains(@href, 'hey')]")

Comments

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.