Python Selenium Webdriver. Writing My Own Expected Condition

How to use 'expected conditions' to check for an element in python-selenium?

Seems you were almost there.

The Documentation clearly says the following:

class selenium.webdriver.support.expected_conditions.visibility_of_element_located(locator)

Which is defined as :

An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0. locator - used to find the element returns the WebElement once it is located and visible

Hence, when you mention:

return EC.visibility_of_element_located((By.XPATH, '//*[@id="kernel_indicator_icon" and @title="Kernel Idle"]'))

The found out WebElement is being returned as follows :

<selenium.webdriver.support.expected_conditions.visibility_of_element_located object at 0x110321b90>

Even the Source Code says the same as :

try:
return _element_if_visible(_find_element(driver, self.locator))

When the search is unsuccessfull :

except StaleElementReferenceException:
return False

How to implement Expected Conditions with Javascript calls in Selenium

You can pass the webelement returned through execute_script() as an argument to the expected_conditions of element_to_be_clickable() and invoke the click as follows:

wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable(driver.execute_script("return document.querySelector("#ember50 > div > div > div:nth-child(1) > div.download-panel > div > div:nth-child(8) > hub-download-card").shadowRoot.querySelector("calcite-card > div > calcite-dropdown > calcite-button")"))).click()

Create custom wait until condition in Python

what I really end up to do is using lambda

self.wait.until(lambda x: waittest(driver, "//div[@id="text"]", "myCSSClass", "false"))

Wrapping Selenium Expected Conditions Python

Eventually I was able to solve this problem after asking this question. So in order to obtain the desired functionality, the above-mentioned method will look like this:

def waitForElement(self, elementName, expectedCondition, searchBy):
try:
if expectedCondition == "element_to_be_clickable":
element = WebDriverWait(self.driver, self.defaultWait).until(EC.element_to_be_clickable((getattr(By, searchBy), elementName)))
elif expectedCondition == "visibility_of_element_located":
element = WebDriverWait(self.driver, self.defaultWait).until(EC.visibility_of_element_located((getattr(By, searchBy), elementName)))

. . .

So it can be called like this:

self.waitForElement("elementName", "element_to_be_clickable", "NAME")

Selenium Expected Conditions - possible to use 'or'?

I did it like this:

class AnyEc:
""" Use with WebDriverWait to combine expected_conditions
in an OR.
"""
def __init__(self, *args):
self.ecs = args
def __call__(self, driver):
for fn in self.ecs:
try:
res = fn(driver)
if res:
return True
# Or return res if you need the element found
except:
pass

Then call it like...

from selenium.webdriver.support import expected_conditions as EC
# ...
WebDriverWait(driver, 10).until( AnyEc(
EC.presence_of_element_located(
(By.CSS_SELECTOR, "div.some_result")),
EC.presence_of_element_located(
(By.CSS_SELECTOR, "div.no_result")) ))

Obviously it would be trivial to also implement an AllEc class likewise.

Nb. the try: block is odd. I was confused because some ECs return true/false while others will throw NoSuchElementException for False. The Exceptions are caught by WebDriverWait so my AnyEc thing was producing odd results because the first one to throw an exception meant AnyEc didn't proceed to the next test.

Selenium Expected Conditions, Wait until element is interactable?

element_to_be_clickable()

element_to_be_clickable() is the expectation for for checking if an element is visible and enabled so that you can click() it.



ElementNotInteractableException

Unfortunately there is no specific expected_conditions as ElementNotInteractableException and it can occur for a lot of reasons and some of them are:

  • Lower Timeout interval. In these cases you have to increase the timeout as follows:

    wait = WebDriverWait(driver, 20)
  • Selecting and invoking click() the outer/parent element rather then the child element.

  • A typical scenario is targetting the <input> where there is a related <label> element.



Related Topics



Leave a reply



Submit