Find element by tag name within element by tag name (Selenium)
<p>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.</p>

<pre><code>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'))
</code></pre>

<p>Thanks in advance.</p>

<p>You have the right idea, but part of your problem is that <code>a_childrens = link.find_element_by_tag_name('a')</code> 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.</p>

<p>Your solution, correctly implemented, might look something like this</p>

<pre><code>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'))
</code></pre>

<p>That is, with the understanding that the HTML layout is as you described, something like:</p>

<pre><code>&lt;li&gt;&lt;a href="foo"&gt;Hello&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="bar"&gt;World!&lt;/a&gt;&lt;/li&gt;
</code></pre>

python selenium