0

I need to get list of links from a webpage it has href="/example/*" it doesn't have any specific class name or id.

elems = driver.find_elements_by_xpath("//a[@href]")
for elem in elems:
    print(elem.get_attribute("href"))

Right now, I'm getting all the links in the page.

1 Answer 1

1

You can either do the filter in the xpath itself (recommended approach)

elems = driver.find_elements_by_xpath("//a[contains(@href,'/example/')]")
for elem in elems:
    print(elem.get_attribute("href"))

or you can fetch and then filter

elems = driver.find_elements_by_xpath("//a[@href]")
for elem in elems:
    href = elem.get_attribute("href")
    if '/example/' in href:
       print(href)
Sign up to request clarification or add additional context in comments.

2 Comments

how about if i wanted to locate it also in specific div? for an example <div class="content"> ... #list is here ... </div>
Then you would use //div[@class='content']//a[contains(@href, '/example/')]'

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.