user6427229
user6427229

Reputation:

python selenium data-style-name

So there's a bit of html that looks like this

<a class="" data-style-name="Black" data-style-id="16360" "true" data-description="null"<img width="32" height="32"

and I was wondering if I could get the text "Black" out of it and than click it, but there's no class name too loop through and the xpath doesn't return anything

Upvotes: 1

Views: 2501

Answers (2)

Arount
Arount

Reputation: 10403

data-style-name is called an attribute of your a element and "Black" is its value.

Here is a way to access attribute's value with selenium & python:

elements = driver.find_elements_by_xpath("//a[@data-style-name]")
for element in elements:
    print element.get_attribute("data-style-name")

If you want to select only elements with attribute data-style-name with value "Black":

driver.find_elements_by_xpath("//a[@data-style-name=Black]")

More about xpath: https://www.w3.org/TR/xpath/#section-Introduction

Upvotes: 2

M. Leung
M. Leung

Reputation: 1701

Have you try on find_element_by_xpath()?

a_check = browser.find_element_by_xpath("/html/body/a[@data-style-name='Black']")

Which returns:

<selenium.webdriver.remote.webelement.WebElement (session="6c94ac24e0ec3a3320ec21b24055f4fa", element="0.1043557711542944-1")>

Upvotes: 0

Related Questions