1

There is a way to use the element_to_be_clickable method by passing as a parameter a webelement and not a locator (By) as can be done in Java.

The problem is because the element I want to wait for until it is clickable is obtained by looking for it in a relative way by xpath in another webelement.

<ul class="my-list">
    <li>Coffee</li>
    <li>Tea
        <div class="acction">
            click-me
        </div>
    </li>
    <li>Milk</li>
    <li>bread
        <div class="acction">
            click-me
        </div>
    </li>
</ul>

My Java code

var items = driver.findElementsByXPath("//ul[@class='my-list']//li");
items.forEach(item -> {
    try {
        var action = item.findElement(By.xpath(".//div[@class='acction']"));
        wait.until(ExpectedConditions.elementToBeClickable(action)).click();
    } catch (NoSuchElementException ignored) {
    }
});

Is there a way I can do the same using selenium in python?

1 Answer 1

2

You can use WebDriverWait and lambda to wait for the simple custom conditions:

wait = WebDriverWait(driver, 5)

parent = driver.find_element_by_css_selector("some element")

wait.until(lambda ignore:
           parent.find_element_by_css_selector("child element").is_displayed()
           and parent.find_element_by_css_selector("child element").is_enabled()
           , "Element is clickable")

Use your own class:

class element_to_be_clickable(object):
    def __init__(self, element):
        self.element = element

    def __call__(self, ignored):
        if self.element.is_displayed() and self.element.is_enabled():
            return self.element
        else:
            return False

wait = WebDriverWait(driver, 5)

parent = driver.find_element_by_css_selector("some element")
wait.until(element_to_be_clickable(parent.find_element_by_css_selector("input")), "Element is clickable")
Sign up to request clarification or add additional context in comments.

2 Comments

I thought that the selenium framework maintains all the functionality of its methods through all the languages it supports, but it seems not, so my only option is to define my own waiting condition. Anyway thank you for answering.
Even though I did that, I still get the same error.

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.