0

I want to click 5 times on an element in selenium how can i do this using for loop or while loop in python i have tried using for loop in python below is the code

nextButton = driver.find_element_by_id("com.sustlabs.ohmassistant:id/btn_next")

for nextButton in range(5):
    nextButton.click()
Anshul Thakur
  • 69
  • 1
  • 6

1 Answers1

1

To click() 5 times on an element using Selenium you can use a _for()_ loop and inducing WebDriverWait for the element_to_be_clickable()as follows:

for i in range(5):
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "com.sustlabs.ohmassistant:id/btn_next"))).click()

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I am assuming the next button is going to a new page so this combined with a try except would be the best option. – Arundeep Chohan Jan 03 '22 at 08:10
  • @ArundeepChohan You are right. But using try, in-case the click is missed, `range(5)` may not click 5 times. Catch 22 situation :) – undetected Selenium Jan 03 '22 at 08:12
  • If we had a lot pages in the range, could we get blocked? Does this method do anything to prevent the server thinking it could be a denial of service attack? It this where an explicit wait would come in? – Martin H Apr 26 '23 at 14:46