Let's say I have multiple divs
looking like this :
<div class="class1">
<div class="class2">
<div class="class3">Text1</div>
<div class="class4">
<a href="https://somelink"><h2>Text2</h2></a>
<p class="class5">Text3 <span class="class6"> Text4 </span></p>
</div>
</div>
</div>
For each div
I can get Text1, Text2, Text3, and Text4:
elements = driver.find_elements_by_xpath("//div[@class='class1']/*")
for e in elements:
print(e.text)
print('------------------------------------------')
But how do I additionaly get value of href
?
I would like to have : https://somelink, Text1, Text2, Text3, Text4
CodePudding user response:
why not do this?
elements = driver.find_elements_by_xpath("//div[@class='class1']/*")
res = []
for e in elements:
res.append(e.text)
href = e.get_attribute('href')
if href is not None:
res.insert(0, href)
print(", ".join(res))
CodePudding user response:
I think you'll find your answer here: Python Selenium - get href value
Basically, it will look like
driver.find_elements_by_css_selector('div.class4 > a').get_attribute('href')