Jesse
Jesse

Reputation: 185

Find element by tag name within element by tag name (Selenium)

I want to print all the href(links) from a website. All these hrefs are stored in an 'a' tag, and these a tags are stored in a 'li' tag. Now, I know how to select all the li's. I need a way to select all the a's within the li's to get the 'href' attribute. Tried the following but doesn't really work.

li = driver.find_elements_by_tag_name('li')
for link in li:
     a_childrens = link.find_element_by_tag_name('a')

for a in a_children
     (print a.get_attribute('href'))

Thanks in advance.

Upvotes: 16

Views: 85819

Answers (5)

Sam Redway
Sam Redway

Reputation: 8117

The accepted answer appears to be out of date now. So if someone comes across this looking for more up to date information as of now with version 4 of Selenium you should use the following syntax:

from selenium.webdriver.common.by import By

url = "https://some-url"
driver.get(url)
elements = driver.find_elements(By.CSS_SELECTOR, "li a")

Upvotes: 0

Ahmed Saad
Ahmed Saad

Reputation: 154

find_element_by_xpath would do the trick...

links = driver.find_elements_by_xpath('//li/a/@href')

Upvotes: 1

afonte
afonte

Reputation: 988

Try to select the links directly:

 links = driver.find_elements_by_tag_name('a')

Upvotes: 9

Buaban
Buaban

Reputation: 5127

I recommend css_selector instead of tag_name

aTagsInLi = driver.find_elements_by_css_selector('li a')
for a in aTagsInLi:
     (print a.get_attribute('href'))

Upvotes: 24

sytech
sytech

Reputation: 40861

You have the right idea, but part of your problem is that a_childrens = link.find_element_by_tag_name('a') will get you what you're looking for, but you're basically throwing out all of them because you get them in the loop, but don't do anything with them as you're in the loop. So you're only left with the variable from the last iteration.

Your solution, correctly implemented, might look something like this

list_items = driver.find_elements_by_tag_name("li")
for li in list_items:
    anchor_tag = li.find_element_by_tag_name("a")
    print(anchor_tag.get_attribute('href'))

That is, with the understanding that the HTML layout is as you described, something like:

<li><a href="foo">Hello</a></li>
<li><a href="bar">World!</a></li>

Upvotes: 5

Related Questions