0

enter image description here

def PID():
record=[]
for page in range(1,10):
    path=get_link('sugar')
    driver.get(path)
    driver.get(path.format(page))
    id=driver.find_elements_by_tag_name('div')
    for i in id:
        results=i.get_attribute('data-id')
        print(results)

I would like get the value of data-id in this div class as shown in the image, the written code prints the data-id along with None Values.

Anurag A S
  • 725
  • 10
  • 23
Hedge
  • 51
  • 5

2 Answers2

1

I'd suggest getting all divs with a data-id field. Getting all divs would get some without the attribute.

id=driver.find_elements_by_xpath("//div[@data-id]")

You can restrict this further by copying the xpath to your parent element in the developer tools inspect element copy xpath and adding it in front of this xpath.

Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32
0

try using ids as your variable name instead of id, because id() is a fundamental built-in of python ('id' is a bad variable name in Python)

def PID():
    record=[]
    for page in range(1,10):
        path=get_link('sugar')
        driver.get(path)
        driver.get(path.format(page))
        ids=driver.find_elements_by_tag_name('div')
        results = [i.get_attribute('data-id') for i in ids]
        print(results)

if you want to append results to record then use:

def PID():
    record=[]
    for page in range(1,10):
        path=get_link('sugar')
        driver.get(path)
        driver.get(path.format(page))
        ids=driver.find_elements_by_tag_name('div')
        results = [i.get_attribute('data-id') for i in ids]
        record.append(results)
Robin
  • 65
  • 2