How to find elemenst under a located element?

Question:

I have a web page something like:

<div class='product-pod-padding'>
 <a class='header product-pod--ie-fix' href='link1'/>
 <div> SKU#1</div>
</div>
<div class='product-pod-padding'>
 <a class='header product-pod--ie-fix' href='link2'/>
 <div> SKU#2</div>
</div>
<div class='product-pod-padding'>
 <a class='header product-pod--ie-fix' href='link3'/>
 <div> SKU#3</div>
</div>

When I tried to loop through the products with the following code, it will give us expected outcome:

products=driver.find_elements_by_xpath("//div[@class='product-pod-padding']")
for index, product in enumerate(products):    
   print(product.text)


SKU#1
SKU#2
SKU#3

However, if I try to locate the href of each product, it will only return the first item’s link:

products=driver.find_elements_by_xpath("//div[@class='product-pod-padding']")
for index, product in enumerate(products):
   print(index)   
   print(product.text)
   url=product.find_element_by_xpath("//a[@class='header product-pod--ie-fix']").get_attribute('href')
   print(url)

   SKU#1
   link1
   SKU#2
   link1
   SKU#3
   link1

What should I do to get the corrected links?

Asked By: yyiws

||

Answers:

This should make your code functional:

[...]
products=driver.find_elements_by_xpath("//div[@class='product-pod-padding']")
for index, product in enumerate(products):
   print(index)   
   print(product.text)
   url=product.find_element_by_xpath(".//a[@class='header product-pod--ie-fix']").get_attribute('href')
   print(url)
[..]

The crux here is the dot in front of xpath, which means searching within the element only.

Answered By: Barry the Platipus

You need to use a relative XPath in order to locate a node inside another node.
//a[@class='header product-pod--ie-fix'] will always return a first match from the beginning of the DOM.
You need to put a dot . on the front of the XPath locator

".//a[@class='header product-pod--ie-fix']"

This will retrieve you a desired element inside a parent element:

url=product.find_element_by_xpath(".//a[@class='header product-pod--ie-fix']").get_attribute('href')

So, your entire code could be as following:

products=driver.find_elements_by_xpath("//div[@class='product-pod-padding']")
for index, product in enumerate(products):
   print(index)   
   url=product.find_element_by_xpath(".//a[@class='header product-pod--ie-fix']").get_attribute('href')
   print(url)
Answered By: Prophet
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.