1

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

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

7 Comments

What is .getAttribute? I says it's not a function
hey this only does it for one of the boxes and I can't click them any idea?
Can you paste somewhere the HTML source of the page please ? Or give the link ? I'm not sure to understand what you are struggling on.
Not sure if I understood, but if you have several items you want to parse in the same page, you can use find_elements_by_xpath (note the "s"), which should returns a list of elements. So you can loop on it and extract all values
ah right of course, should've thought of that. Thanks
|
0

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")>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.