Drake491 Drake491 - 19 days ago 5
HTML Question

Python - Return ID after finding Xpath with Selenium

I'm learning xpath and Selenium, and just successfully found the element I am trying to target. I am searching for a element where data-number = 48264

driver.find_element_by_xpath("//*[@data-number='48264']")


The HTML I am pulling from looks like this

<li id="pos_4" data-number="48264" class="top-item sellmid">


Now I am trying to return the ID "pos_4". I experimented with the below code, but it was totally wrong.

driver.find_element_by_xpath("//*[@data-asin='B01923Y7IG']").id


This returned a long float "0.066103113793198-1". Not sure what this is.

driver.find_element_by_xpath("//*[@data-asin='B01923Y7IG']").text


The above returned the text inside the div of the li.

Any suggestions on where to look next? I've done a lot of searching, but couldn't find anything helpful. Thanks!

Answer

you can do that by

ele = driver.find_element_by_xpath("//*[@data-number='48264']")

value = ele.get_attribute('id')

print value