2

I want to run

driver.find_element_by_css_selector("MY_SELECTORS").click()

inside a test, but there are times that the element does not exist. if I did:

return = driver.find_element_by_css_selector("MY_SELECTORS").click()

and the element does not exist, would a value ( like an exception , or boolean FALSE) be returned? I have been reading Docs » Locating Elements, but although it details the error types, its not clear if a value is returned.

Does anyone have experience with this?

1 Answer 1

5

Your statement has two parts, locating and clicking, there will be different exceptions and return types.

find_element_by_css_selector() returns webelement and may throw exception, while click() is void, exception might also be thrown.

So for example, if your locator is valid but results in no matching element, NoSuchElementException should be thrown. If your element is found, but not in clickable state, some type of exception will be thrown.

# untested Python pseudo code, only provides the logic
try:
    driver.find_element_by_css_selector("Selector doesn't exist").click()
except ElementNotVisibleException:
    print "ElementNotVisibleException"
except NoSuchElementException:
    print "NoSuchElementException"
except InvalidSelectorException:
    print "InvalidSelectorException"
except:
    print "Other exception types possible"
    raise
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that's very helpful! For my own information when you say "returns webelement and may throw exception" This cannot be captured in a variable ?
Return type and exception are different, why do you want to do that? It depends on what you want to get. You can have a method to return boolean or webelement.

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.