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.
CodePudding user response:
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]
CodePudding user response:
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
CodePudding user response:
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()")
CodePudding user response:
Here can help you solve your problem https://exe.io/p4wPy