Selenium xpath element attribute Selenium xpath element attribute selenium selenium

Selenium xpath element attribute


I would rely on the presence of data-nice_url attribute and a vehicle class:

vehicle = driver.find_element_by_xpath('//div[@data-nice_url and contains(@class, "vehicle")]')print(vehicle.get_attribute("data-nice_url")

With WebDriverWait applying to your code:

wait = WebDriverWait(driver, 30)car_links = wait.until(lambda driver: driver.find_elements_by_xpath('//div[@data-nice_url and contains(@class, "vehicle")]'))for car in carLinks:    print car

And, as an alternative, a CSS selector:

vehicle = driver.find_element_by_css_selector('div.vehicle[data-nice_url]')print(vehicle.get_attribute("data-nice_url")


You can first get an element by XPath and then use WebElement's method get_attribute to retrieve the information you need.

Example:

element = driver.find_elements_by_xpath(urlXpath)nice_url = element.get_attribute("data-nice_url")


urlXpath = //div[@class="car-thumb-item clickable vehicle"] nice_url = driver.find_element_by_xpath(urlXpath).get_attribute("data-nice_url")