Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to efficiently iterate variable number in Selenium Webdriver using Python?

I am using selenium webdriver with Python on IE and the code mentioned below is working fine but I need to make this in a loop. Please suggest. termsName0 may be 2 ,3 4 or N numbers

Code:

Select(self.driver.find_element_by_name("termsName0")).select_by_visible_text("Blank No Text")
Select(self.driver.find_element_by_name("termsName1")).select_by_visible_text("Blank No Text")
Select(self.driver.find_element_by_name("termsName3")).select_by_visible_text("Blank No Text")
like image 442
user3702349 Avatar asked Apr 25 '26 11:04

user3702349


1 Answers

You can use try except contract if you do not know the "N".

from selenium.common.exceptions import ElementNotVisibleException, WebDriverException, NoSuchElementException

try:
    i = 0
    while True:
        name = "termsName" + str(i)
        Select(self.driver.find_element_by_name(name).select_by_visible_text("Blank No Text")
        i +=1
 except (ElementNotVisibleException, WebDriverException, NoSuchElementException):
        pass
like image 140
Shashank Singh Avatar answered Apr 27 '26 23:04

Shashank Singh