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()
1
  • Do you see any error? Update the question with the relevant HTML of the Next button. Commented Jan 3, 2022 at 7:56

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

3 Comments

I am assuming the next button is going to a new page so this combined with a try except would be the best option.
@ArundeepChohan You are right. But using try, in-case the click is missed, range(5) may not click 5 times. Catch 22 situation :)
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.