新手区 appium 的 Python 版中,使用 WebDriverWait。。。until。。。做元素等待,报参数个数错误

江海 · 2016年05月18日 · 最后由 yifenzhonghuxi 回复于 2016年05月20日 · 2868 次阅读

1.代码如下:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By

WebDriverWait(app_driver,5).until(presence_of_element_located(By.ID,'com.miutrip.android:id/bbn_item1'))

2.报错如下:
WebDriverWait(app_driver,5).until(presence_of_element_located(By.ID,'com.miutrip.android:id/bbn_item1'))
TypeError: init() takes exactly 2 arguments (3 given)

3.已经看过如下:
selenium 中关于 wait 的文档:http://selenium-python.readthedocs.io/waits.html
看过 presence_of_element_located() 类源码,其下只有initcall两个方法,均只有一个参数
看过 WebDriverWait(app_driver,5).until() 源码,使用没有错误

共收到 10 条回复 时间 点赞

示例里面的 wait 是来自 selenium.webdriver.support.ui 的,和你的不一样。

from selenium.webdriver.support.ui import WebDriverWait

#1 楼 @chenhengjie123 我错了,应该不是这个问题。不知道你的 presence_of_element_located 是怎么 import 的?能不能把完整的代码贴上来?

我直接用官网的示例是没问题的:

#!/usr/bin/env python
# -*- coding: utf-8 -*-


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

driver = webdriver.Firefox()
driver.get("http://testerhome.com")
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "navbar-header"))
    )
finally:
    driver.quit()

PS:麻烦正文用 markdown 排版。。。

#1 楼 @chenhengjie123 我改成 from selenium.webdriver.support.ui import WebDriverWait,依然是报一样的错误。。。。

#3 楼 @jh901011 忽略 1 楼,请看 2 楼的

#2 楼 @chenhengjie123
下面是完整的代码:
我测的是无限商旅 APP,id 是登录页面的登录按钮

from appium import webdriver
from appium.webdriver.webelement import WebElement
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By

desired_caps = {}

desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.0'
desired_caps['deviceName'] = 'QST8EIS499999999'
#desired_caps['app'] = 'E:\WDJDownload\Apps\wuxianshanglv.apk'
desired_caps['appPackage'] = 'com.miutrip.android'
desired_caps['appActivity'] = 'com.miutrip.android.SplashActivity'

app_driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)
try:
    WebDriverWait(app_driver,5).until(expected_conditions.presence_of_element_located(By.ID,'com.miutrip.android:id/btn_login'))
finally:
    app_driver.quit()

还是有区别的。是语法错误。

##正确的
WebDriverWait(app_driver, 5).until(
    expected_conditions.presence_of_element_located((By.ID, 'com.miutrip.android:id/btn_login')),
    message='not find')
##你的
WebDriverWait(app_driver, 5).until(
    expected_conditions.presence_of_element_located(By.ID, 'com.miutrip.android:id/btn_login'))

找个好的编辑器是看的舒服点。
有强迫症的人,写几行都要格式化下。。。

#6 楼 @among29 我试了,可以不加 message,因为 Python 里面定义 presence_of_element_located 这个方法的时候,默认 message=‘’。只需要把前面的 By.id 及具体元素括起来作为元组,传给 located 方法,就好了。是我弄混了,谢谢啦!!

#4 楼 @chenhengjie123 谢谢啦!我知道怎么做了,把 By.id 及具体元素括起来作为元组,传给 located 方法,就好了。感谢感谢!

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册