1

I'm using Selenium to scrape a Web Page and I'm having some problems targeting some attributes.

The page I'm trying to scrape looks like this:

<div>
    <span abc> content </span>
    <span def> content2 </span>
<div>

My goal would be to retrieve the text within the "span abc" tag, without selecting the other text included in the "span def" tag.

I've tried multiple approaches and looked at a lot of different resources but I wasn't able to find the right approach, since I don't want to select all the spans at the same time and I don't want to search based on the text within the tags.

3 Answers 3

1

A simple approach would be indexing cause you do not want to select based on

since I don't want to select all the spans at the same time and I don't want to search based on the text within the tags.

If abc is an attribute please use :

//div/span[@abc]

or

with indexing :

(//div/span[@abc])[1]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks , your first line is exactly what I was looking for.
0

If you only want to pull the first span out of these two, you could easily do this with the XPATH. It would look like this:

span = driver.find_element_by_xpath("/html/body/div/span[1]").text

if you want to pull every span, but execute commands with each of these you could do:

span = len(driver.find_elements_by_xpath("/html/body/div/span"))
m = 1

while m <= 0:
    span = driver.find_element_by_xpath("/html/body/div/span["+str(m)+"]")
    print(span.text)

    m = m + 1

Comments

0

You can use xpath like //span[1]/text() for get text inside of the <span> tag

span = driver.find_element_by_xpath("/html/body/div/span[1]/text()")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.