1

I have an if else statement nested in a for loop and I am trying to figure out a way to only run the program for the current feature (u) if the element exists. The element in question is a value in a select/option format. Basically, I need a certain option in that drop down menu to be present for the code to run and if it is not I would like to move onto the next url and try again. The option is either present or it's not (it won't pop up if the driver waits).

for u in urls:
    bot.get(u)
    if *statement to check if the value exists*:
       -- runs my intended function --
    else:
        print("This site is not reporting yet")

Thank you.

1 Answer 1

1

You need to loop through the urls and check if the value exists within a try-catch{} block and you can use the following solution:

for u in urls:
    try:
        bot.get(u)
        #check if the value exists using WebDriverWait with expected_conditions as visibility_of_element_located
        #runs my intended function
    except TimeoutException as e:
        print("This site is not reporting yet")
        continue
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the answer. To follow up, I have "from selenium.webdriver.common.by import By", "from selenium.webdriver.support.ui import WebDriverWait" and "from selenium.webdriver.support import expected_conditions as EC". I'm getting a few errors because the "type object 'By' has no attribute 'Value' "(The dropdowns only have Values, no IDs or Names), and a NameError because "name 'TimeoutException' is not defined". Am I running it as EC.visibility_of_element_located((By.Value, "my_value"))? Or bot.visibility_of_...?
I actually believe I just fixed the TimeoutException error with from selenium.common.exceptions import TimeoutException, But now I'm also wondering if it will be a "TimeoutException" or a "NoSuchElementException".
WebDriverWait on failure will always raise TimeoutException, so you need selenium.common.exceptions import TimeoutException

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.