简体   繁体   中英

Selenium Python - how to get deeply nested element

I am exploring Selenium Python and trying to grab a name property from Linkedin page in order to get its index later.

This is the HTML:

在此处输入图像描述

Here is how I try to do it:

all_span = driver.find_elements(By.TAG_NAME, "span")

all_span = [s for s in all_span if s.get_attribute("aria-hidden") == "true"]

counter = 1
for i in all_span:
    print(counter)
    print(i.text)
    counter += 1

The problem is there are other spans on the same page that also have aria-hidden=true attribute, but not relevant and that messes up the index.

So I need to reach that span that contains name from one of its its parent divs but I don't know how. Looking at documentation here: https://selenium-python.readthedocs.io/locating-elements.html# I cant seem to find how to target deeply neseted elements.

I need to get the name that is in span element.

The link

The best way would be to use xpath. https://selenium-python.readthedocs.io/locating-elements.html#locating-by-xpath

Let's say you have this:

 <div id="this-div-contains-the-span-i-want"> <span aria-hidden="true"> <.--... //--> </span> </div>

Then, using xpath:

xpath = "//div[@id='this-div-contains-the-span-i-want']/span[@aria-hidden='true']"
span_i_want = driver.find_element(By.XPATH, xpath)

So, in your example, you could use:

xpath = "//a[@class='app-aware-link']/span[@dir='ltr']/span[@aria-hidden='true']"
span_i_want = driver.find_element(By.XPATH, xpath)
print(span_i_want.text)

No typos but

print(span_i_want) - returns [] empty array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM