E. hendrix
E. hendrix

Reputation: 29

How do I get the class name from HTML using the absolute xpath

In the following picture, the full xpath of the yellow highlighted bit of HTML is

/html/body/bx-site/ng-component/div/sp-sports-ui/div/main/div/section/main/sp-path-event/div/sp-next-events/div/div/div[2]/div1/sp-coupon1/sp-multi-markets/section/section/sp-outcomes/sp-two-way-vertical[2]/ul/li1/sp-outcome/button

I am using selenium to scrape some data from a website. The text of the xpath is what I want but I also need the class name of the yellow highlight bit of HTML. The class name constantly changes so I need a way to retrieve the class name along with the text. In this case the class name would be "bet-btn". I am using driver.find_element_by_xpath to get the text from the html, but can't figure out a way to retrieve the class name. Using the xpath is there a way in selenium to retrieve the class name of the yellow highlighted bit.

html code

Upvotes: 0

Views: 694

Answers (1)

JD2775
JD2775

Reputation: 3801

I would advise against using absolute xpath unless you really needed to

Try this instead:

elem = driver.find_element_by_xpath("//sp-outcome/button")
class_value = elem.get_attribute("class")

BTW that xpath is assuming there are no other //sp-outcome/button element paths on that page. If there are you would need to expand it some, but you still wouldn't need the entire absolute xpath. Those are generally pretty fragile.

Upvotes: 1

Related Questions