slender
slender

Reputation: 49

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

Upvotes: 1

Views: 413

Answers (2)

KunduK
KunduK

Reputation: 33384

To get the value of data-bk and data-odig from all td tags

First you need to use find_elements_by_css_selector() which will return list of elements and then you need to iterate

Secondly to get the attribute value you need to use element.get_attribute("attributename")


Code:

Databk=[item.get_attribute("data-bk") for item in driver.find_elements_by_css_selector("td[data-bk][data-odig]")]
DataOdig=[item.get_attribute("data-odig") for item in driver.find_elements_by_css_selector("td[data-bk][data-odig]")]
print(Databk)
print(DataOdig)

This will return list of data-bk and data-odig

Upvotes: 1

PDHide
PDHide

Reputation: 19989

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

**

Upvotes: 0

Related Questions