0

I have a website with such class:

<html>
    <body><div class="class1" data-ref="data"></div></body>
<html>

I want to use selenium in python 3 to get the data-ref (i.e. "data").

I know you can get the text using .text is there something like that for data-ref?

raga raga
  • 37
  • 4

3 Answers3

1

Just do this:

get_attribute("attribute name")

In your case

get_attribute("data-ref")
Norayr Sargsyan
  • 1,737
  • 1
  • 12
  • 26
0

You can first find the element using its xpath and then can find the data-ref value using get_attribute method.

You can do it like:

element = driver.find_element_by_xpath("//div[@class='class1']")
# Find the value of data-ref
value = element.get_attribute("data-ref")
Sameer Arora
  • 4,439
  • 3
  • 10
  • 20
-1

To retrieve the value of data-ref attribute i.e. data you can use either of the following Locator Strategies:

  • Using XPATH:

    print(driver.find_element_by_xpath("//div[@class='class1']").get_attribute("data-ref"))
    
  • Using CSS_SELECTOR:

    print(driver.find_element_by_xpath("div.class1").get_attribute("data-ref"))
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352