I am trying to get a timestamp from a website but element can't be located. The b-gray-800 is a child from stories-list
details = driver.find_element("xpath","//div[@id='stories-list']")
res = details.find_elements("xpath","./div[@class = 'b-gray-800']")
for items in res:
print(items.get_attribute('data-story-timestamp'))
I dont know what I am doing wrong
<div id="stories-list">
<div data-story-style="bordered" data-story-timestamp="1667211688511493" data-story-type="picture_created" >
CodePudding user response:
If you are using xpath to locate an element, you dont have to transverse to that element, you can directly access that element no matter how deep it is.
Also, you are using deprecated methods in selenium.
For your case, I recommend using IDs and Classes to reach your goal:
details = driver.find_element(By.ID,"stories-list")
res = details.find_elements(By.CLASS_NAME,"b-gray-800")
for items in res:
print(items.get_attribute('data-story-timestamp'))
It should work.
CodePudding user response:
I was looking for a similar thing but when I try this I got the error: details = driver.find_element(By.ID,"stories-list") ^^ NameError: name 'By' is not defined