Python selenium: wait until element is clickable - not working
Asked Answered
O

4

58

I will test a web-app. there is a button available in my table to select all entries. I've tried:

driver.wait.until(ExpectedCondition.element_to_be_clickable((By.XPATH, "myXpath"))).click()

selenium clicks on the button, but nothing happens. (also with send_Keys(Keys.Return)) the application is developed with GXT, I thing that there is much javascript behind the button. Is there is possibility to wait until a eventloader is ready? waiting before a click solves the problem, but not a solution for automated testing.

Orangutan answered 23/1, 2015 at 12:32 Comment(1)
you'll need to share the code or url to get thorough assistance. can you do that?Botanomancy
F
114

The correct syntax for an explicit wait in Python using a Selenium driver is:

element = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.ID, "myElement")))

Better that After above you do : element.click();

So in your case :

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "myXpath")))

element.click();

Better you follow it. Also, share your whole code so that I may correct it. Just 1 line of code is a little confusing to understand.

Forewent answered 23/1, 2015 at 12:38 Comment(4)
can you imagine that WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "myXpath"))).click() gives the same error for me? btw, did you notice the semicolon?Tollmann
Not work for me too - -Nibbs
Not for me either :)Ark
What if we don't have the element xpath but we have the element itself?Tollmann
I
6

I had also that problem... Web apps have views over views and Appium sometimes gets wrong.

This worked for me:

x = webElement.location['x'] + (webElement.size['width']/2)
y = webElement.location['y'] + (webElement.size['height']/2)
print("x: "+x+" - y: "+y)

//I have setted a 200 milli duration for the click...
//I use tap just for Android... If is iOS for me it works better touchAction
driver.tap([(x,y)], 200)

Edit:

I misunderstood your question... Sorry... Maybe modifying your Xpath to: (don't know if this will work at a web app)

xpath = "//whatever_goes_here[@clickable='true']"
Incrocci answered 1/9, 2017 at 10:34 Comment(0)
N
2

I know it is probably too late, but for me the solution was to add this line before all the elements clicks:

driver.execute_script('document.getElementsByTagName("html")[0].style.scrollBehavior = "auto"')

Nowadays, sites tend to have a scrholl-behavior set to auto. Drivers do not know that, though they do know when an element is outside the view. So, what happens is a driver tries to click the element. The driver sees that the element is outside the view, so it calls a scroll method and after that immediately clicks the element without waiting for scrolling to finish. And the scrolling does take some time because of its behavior set to auto.

Nerland answered 27/9, 2022 at 17:40 Comment(0)
S
0

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait=WebDriverWait(driver,5)
a= wait.until(EC.element_to_be_clickable(('id or xpath or class or any thing else ','enabled_trigger')))
a.click()

please note you must have two parentheses in element.to_be_clickable(())

Samhita answered 11/10, 2022 at 0:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.