3

With selenium in python i want to click on a html div container if it contains some words and if it can't find any the script must exit. With the below code it is working if there is a div containing a word from the text list but how do i exit where non of the words is found? With the code below it executes order.click because this is outside the for loop. I only want to execute order.click() and go further with the rest of the script break if words are found

    text = ["Dog", "Cat", "Bird"]

    for word in text:
        try:
            order = WebDriverWait(driver,5).until(EC.presence_of_element_located((By.XPATH, "//div/p[contains(text(),'{}')]".format(word))))
            if order != None:
                print(f"found div with word: {word}")
                break
        except:
            print(f"did NOT found div with word: {word}")

    order.click()
 
  # and more commands after this....

2 Answers 2

2
import sys

And in the except:

    text = ["Dog", "Cat", "Bird"]
    is_found = False
    for word in text:
        try:
            order = WebDriverWait(driver,5).until(EC.presence_of_element_located((By.XPATH, "//div/p[contains(text(),'{}')]".format(word))))
            if order != None:
                print(f"found div with word: {word}")
                is_found = True
                break
        except:
            print(f"did NOT found div with word: {word}")

    if is_found == False:
        sys.exit()
    order.click()
 
  # and more commands after this....

This is if you want to exit the whole script.

If you just want to not run order.click(), Then move this line in your try scope:

try:
            order = WebDriverWait(driver,5).until(EC.presence_of_element_located((By.XPATH, "//div/p[contains(text(),'{}')]".format(word))))
            if order != None:
                print(f"found div with word: {word}")
                order.click()
                break
Sign up to request clarification or add additional context in comments.

3 Comments

thanks but now the script will close immediately as soon as the first is not found without checking the other two. What it has to do is close script only when NONE of the words are found. It must execute order.click for e.g. if Bird is found but there is no div with Dog or Cat
Got your problem, just edited the answer.
Yes thanks a lot! this will do the trick :)
1

I think you need to include the "order.click()" in the indented part of the code that check if the item is found in the list, like below.

text = ["Dog", "Cat", "Bird"]

for word in text:
    try:
        order = WebDriverWait(driver,5).until(EC.presence_of_element_located((By.XPATH, "//div/p[contains(text(),'{}')]".format(word))))
        if order != None:
            print(f"found div with word: {word}")
            order.click()
            break
    except:
        print(f"did NOT found div with word: {word}")

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.