4

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")

3 Answers 3

1

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

Comments

0

This is one way. N specifies how many times you want to loop

N = 4

for i in range(N):
    name = "termsName" + str(i)
    Select(self.driver.find_element_by_name(name).select_by_visible_text("Blank No Text")

Basically it takes the number of the current iteration and appends that to "termsName".

Above code is equivalent to

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("termsName2")).select_by_visible_text("Blank No Text")
Select(self.driver.find_element_by_name("termsName3")).select_by_visible_text("Blank No Text")

6 Comments

Thank you for quick response, but I dont know the N = 4 , this may change on UI , some times 1 some times 6 . In that case If I defined N =4 then above code will fail.
@user3702349 how is N acquired? You can change N. You just need to set N equal to the number that you get from the UI
Actually on my page value for N will vary.
@user3702349 I know. You have a piece of code that gets the value for N from the ui? You need to assign N to that value
Last query:Actually on my page value for N will vary. Would it be possible to first check termsname and then take out the value of N and then use Loop.
|
0

For iterating variable number of items you could try something like this

#To fetch all elements start with termsName 
element_list = firefox_driver.find_elements_by_id("termsName[0-9]*")

# below code will iterate over the all links
for i in range(len(element_list)):
        name = "termsName" + str(i)
        Select(self.driver.find_element_by_name(name).select_by_visible_text("Blank No Text")

Hope it helps.

Comments

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.