Void S
Void S

Reputation: 802

XPath- XPath is Printing Repeating Value

I want to get the "data-id" value for each row of this webpage.

I copied the XPath of the first row, however, it just prints the value of the "data-id" from the first row x times. Expected result should be the data-id of each row gets its own data-id printed enter image description here

Expected Output- "514" "515" "516" ... ...

import time
from selenium import webdriver
driver = webdriver.Chrome()

driver.get('https://www.abstractsonline.com/pp8/#!/10517/sessions/@timeSlot=Apr08/1')
page_source = driver.page_source
element = driver.find_elements_by_xpath('.//li[@class="result clearfix"]')
for el in element:
    id=el.find_element_by_xpath('/html/body/div[1]/div[2]/div/div/div[2]/div[2]/div/div[2]/div[2]/ul/li[1]/div[1]/div[1]/h1').get_attribute("data-id")
    print(id)

Upvotes: 1

Views: 62

Answers (1)

Gaurav Lad
Gaurav Lad

Reputation: 1808

Try something like this:

import time
from selenium import webdriver
driver = webdriver.Chrome()

driver.get('https://www.abstractsonline.com/pp8/#!/10517/sessions/@timeSlot=Apr08/1')
//page_source = driver.page_source
element = driver.find_elements_by_xpath('.//li[@class="result clearfix"]//h1')
for el in element:
    id=el.get_attribute("data-id")
    print(id)

Upvotes: 1

Related Questions