3

This is my first question so if I missed some information or did anything wrong I already apologize for that. I am using a ChromeDriver to scrape through the Internet. The used language is Python in combination with Selenium.

I have an element which is a span tag that I stored in a variable called elem. I could find the parent element of elem

articles.append(elem.find_parent("a")['href'])

which was stored in that array articles. Now I need to find the ancestor element which is the span tag with the class saying "price": lowest line represents elem and the second line is the element I am looking for.

However, trying the same like before:

elem.find_parent("span")['class']

doesn't work out for me and I get a Nonetype Error. I have tried multiple other methods but always got a NoneType Error.

Thank you in advance.

vitaliis
  • 4,082
  • 5
  • 18
  • 40
delamain
  • 73
  • 2
  • 7

2 Answers2

5

Parent element

Parent selects the parent of the current node.

Lets take as an example the reputation span element from Stackoverflow user's profile: To go one level up with XPATH:

//span[@class='grid--cell ']

Solution 1

You can go one level up with /..

//span[@class='grid--cell ']/..

Solution 2

Using XPATH parent :

//span[@class='grid--cell ']/parent::div[@class='grid gs8 fs-headline1']

parent selects the parent of the current node

Solution 3

Using XPATH ancestor

//span[@class='grid--cell ']/ancestor::div[@class='grid gs8 fs-headline1']

The difference is that ancestor selects not only the parent, but also grandparents and so on of the current element.

There is no a good way currently to do the same with CSS.

To find child elements I usually use // for all siblings or / for a direct sibling.

vitaliis
  • 4,082
  • 5
  • 18
  • 40
  • 1
    I'm gonna show you a little trick to select a Parent tag that contains a span class. //div[@class='grid gs8 fs-headline1'][.//span[@class='grid--cell ']]. – Arundeep Chohan Apr 25 '21 at 05:28
  • I dont have access to the xpath, I am using beautiful soup and have every element in an array. Now I want to go to each element and find that specific parent tag. I dont have the xpatht though. – delamain May 04 '21 at 12:08
  • There is still probably no way to do this with css selectors. Check here https://stackoverflow.com/questions/1014861/is-there-a-css-parent-selector – vitaliis May 04 '21 at 12:10
3
from selenium import webdriver
driver = webdriver.Chrome(executable_path="
C:\\chromedriver.exe")
driver.implicitly_wait(0.5)
driver.get("https://www.tutorialspoint.com/about/about_careers.htm")
#identify child element
l= driver.find_element_by_xpath("//li[@class='heading']")
#identify parent from child element with (..) in xpath
t= l.find_element_by_xpath("..")
# get_attribute() method to obtain class of parent
print("Parent class attribute: " + t.get_attribute("class"))
driver.close()

Solution from https://www.tutorialspoint.com/how-to-find-parent-elements-by-python-webdriver

Allex Radu
  • 1,257
  • 13
  • 24