1

I'm trying to web scrape different elements with same class name. The following statements works well.

browser.find_element(By.XPATH, "(//div[@class= 'jumbo-tracker'])[1]").click()
browser.find_element(By.XPATH, "(//div[@class= 'jumbo-tracker'])[2]").click()

... and so on.

Now, if I put this in a loop, it doesn't work. Looks like, it doesn't recognize (//div[@class= 'jumbo-tracker'])[i]

Here's the code:

for i in range(1,length+1):
    browser.find_element(By.XPATH, "(//div[@class= 'jumbo-tracker'])[i]").click()
    sleep(5)
    browser.find_element(By.XPATH, "//div[@class='sc-1y3q50z-3 eiMLBn']").click()
    sleep(5)
    sno.append(restaurant)
    restaurant_name= browser.find_element(By.XPATH,"//h1[contains(@class, 'sc-7kepeu-0 sc-kafWEX kTxZkY')]").text
    name.append(restaurant_name)
    browser.back()
    browser.back()
    sleep(5)

Here's the exception:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"(//div[@class= 'jumbo-tracker'])[i]"}

Please, help.

Afreen Sofi
  • 9
  • 1
  • 3

3 Answers3

1

You can click all elements keep in for loop and you have to use elements instead of element.

clicks = browser.find_elements(By.XPATH, "//div[@class= 'jumbo-tracker']")

for click in clicks:
   click = click.click()
   time.sleep(2)
Md. Fazlul Hoque
  • 15,806
  • 5
  • 12
  • 32
0

Use string interpolation. String interpolation is a process substituting values of variables into placeholders in a string. This example (based on your code) uses an f-string:

for i in range(1,length+1):
    browser.find_element(By.XPATH, f"(//div[@class= 'jumbo-tracker'])[{i}]").click()
DSteman
  • 1,388
  • 2
  • 12
  • 25
0

range()

The range() function creates a sequence of numbers.

So while passing the variable within the xpath using f-strings you need to convert the number as string for variable substitution and you can use the following line of code:

for i in range(1,length+1):
        browser.find_element(By.XPATH, f"(//div[@class= 'jumbo-tracker'])[{str(i)}]").click()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352