Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

python - Scraping other elements from a tag in Selenium

The script on the webpage reads:

<td class="bc bs o" data-bk="B3" data-odig="7.5" data-o="13/2" data-hcap="" data-fodds="7.5" data-ew-denom="4" data-ew-places="5"><p>7.5</p></td>

I want to extract the 'data-bk' and 'data-odig' values for all td tags in the driver in Selenium. I know that every td tag has 'data-bk' and 'data-odig' values but I don't know what they are ("B3" "7.5") for each tag. I want to print the list of 'data-bk' values ("B3" etc.). I tried the following:

answer = driver.find_element_by_css_selector('td[data-bk]')
print(answer.text)

But this does not work

question from:https://stackoverflow.com/questions/65919837/scraping-other-elements-from-a-tag-in-selenium

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
driver.find_element_by_xpath('//td[@data-bk="B3"]')
driver.find_element_by_css_selector('td[data-bk="B3"]')

you can use css or xpath

xpath syntax is

//tag[@attribute="attribute-value"]

CSS syntax is

tag[attribute="attribute_value"]

**

so you can use any attribute not only id or name

**


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...