Home > Software engineering >  xpath to check only within WebElement in Selenium / Python
xpath to check only within WebElement in Selenium / Python

Time:04-02

I am very new to this and i have tried to look for the answer to this but unable to find any.

I am using Selenium chromedriver, trying to monitor some items I am interested in.

Example: a page with 20 items in a list.

Code: #list of items on the page

search_area = driver.find_elements_by_xpath("//li[@data-testid='test']")

search_area[19].find_element_by_xpath("//p[@class='sc-hKwDye name']").text

  • this returns the name of item[0]

search_area[19].find_element_by_css_selector('.name').text

  • this returns the name of item[19]

why is xpath looking at the parent html?

I want xpath to return the name of item within the WebElement /list item. is it possible?

CodePudding user response:

found the answer, add a . in front

hope this is gonna help someone new like me in the future.

from search_area[19].find_element_by_xpath("//p[@class='sc-hKwDye name']").text

to search_area[19].find_element_by_xpath(".//p[@class='sc-hKwDye name']").text

CodePudding user response:

What you are passing in 'find_element_by_xpath("//p[@class='sc-hKwDye name']")' is relative Xpath. You can pass the full Xpath to get the desired result.

  • Related