Python selenium:等到元素可点击 - 不工作

问题描述 投票:20回答:2

我将测试一个网络应用程序。我的表格中有一个按钮可供选择所有条目。我试过了:

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

selenium点击按钮,但没有任何反应。 (还有send_Keys(Keys.Return))应用程序是用GXT开发的,我的事情是按钮背后有很多javascript。是否有可能等到事件加载器准备好了?在点击之前等待解决问题,但不是自动化测试的解决方案。

python selenium selenium-webdriver webdriver click
2个回答
46
投票

在python中显式等待的正确语法是:

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

更好的是在上面你做了:element.click();

所以在你的情况下:

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();

你更好地遵循它。也分享你的整个代码,以便我可以纠正它。你的1行代码很少混淆。


2
投票

我也遇到了这个问题...... Web应用程序可以查看视图,而Appium有时会出错。

这对我有用:

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)

编辑:

我误解了你的问题...抱歉...也许修改你的Xpath :(不知道这是否适用于网络应用)

xpath = "//whatever_goes_here[@clickable='true']"
© www.soinside.com 2019 - 2024. All rights reserved.