'Detect a tag using selenium
When I use
thumbnails = driver.find_element(By.CLASS_NAME, 'thumbnails')
and iterate through it's children, how can I detect the tag like div or a? Example structure:
<div class="thumbnails">
<div class="section">Section 1</div>
<a href="#">Link 1.1</a>
<a href="#">Link 1.2</a>
<a href="#">Link 1.3</a>
<a href="#">Link 1.4</a>
<div class="section">Section 2</div>
<a href="#">Link 2.1</a>
<a href="#">Link 2.2</a>
<div class="section">Section 3</div>
<a href="#">Link 3.1</a>
<a href="#">Link 3.2</a>
<a href="#">Link 3.3</a>
<a href="#">Link 3.4</a>
<a href="#">Link 3.5</a>
<a href="#">Link 3.6</a>
<a href="#">Link 3.7</a>
</div>
I need to create a json object like below:
[
{
"section_name": section_name,
"links": a_tags_list
}
]
Solution 1:[1]
thumbnails = driver.find_element(By.CLASS_NAME, 'thumbnails')
a_tags_list=[x.text for x in thumbnails.find_elements(By.TAG_NAME,'a')]
Not sure if you want the text or get_attribute('outerhtml')
But you could just use the element thumbnails and find elements from there.
If you want to grab all the children with the tags div or a tag.
allChildren=thumbnails.find_elements(By.XPATH,'./a | ./div')
you can then loop this and check if it has an href attribute.
for child in allChildren:
if child.get_attribute('href'):
a_tags_list.append(child.get_attribute('href'))
else:
#stuff
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
