Hello I am new to python and was wondering if anyone could help me understand how to get a value using selenium and some terminology in HTML to help future searches.
I am trying to take a value that changes from page to page out of a div tag and assign it to a value.
Here is the HTML I am searching:
<div data-current-step-index="4">
I am trying to get that 4 into a value to print, this is what I have so far:
element = driver.find_element_by_xpath("//div[contains(@class, 'data-current-step-index')]")
print(element)
Any help would be appreciated!
CodePudding user response:
elem=driver.find_element(By.XPATH,"//div[@class='order-progress-bar']")
print(elem.get_attribute("data-current-step-index"))
Get the element you want and print it's attribute. Also stop using driver.find_element_by_xpath it's depreciated.
You could also use @data-current-step-index='4' if there are several or just wrap it (xpath)[index] and get a certain one.
Import:
from selenium.webdriver.common.by import By