Twiggy Garcia
Twiggy Garcia

Reputation: 37

how do I loop through an xpath target adding 22 each time - python

I have a list of items that I want to scroll through, it is 22 long, then it dynamically loads another 22 items up until 80000.

how do I loop through this list adding 22 each time to the xpath [22] until I reach the goal of 80000

scrollTarget = driver.find_element_by_xpath('//*[@id="userListModalFollowers"]/div/div/div[22]')\

driver.execute_script('arguments[0].scrollIntoView()', scrollTarget)

sleep(5)

scrollTarget = driver.find_element_by_xpath('//*[@id="userListModalFollowers"]/div/div/div[44]')\

driver.execute_script('arguments[0].scrollIntoView()', scrollTarget)

Upvotes: 0

Views: 71

Answers (1)

AaravM4
AaravM4

Reputation: 410

You could just use a loop in Python skipping by an interval of 22 like so:

base_xpath = '//*[@id="userListModalFollowers"]/div/div/div['

for i in range(22, 80000, 22): # skipping by 22 each time
  xpath = base_xpath + str(i) + ']'

  scrollTarget = driver.find_element_by_xpath(xpath)

  driver.execute_script('arguments[0].scrollIntoView()', scrollTarget)

  sleep(5)

# Scroll to the last 8 entries
xpath = base_xpath + "80000" + ']'
scrollTarget = driver.find_element_by_xpath(xpath)
driver.execute_script('arguments[0].scrollIntoView()', scrollTarget)

Upvotes: 1

Related Questions