TypeError:__ init __()需要2个位置参数,但是给出了3个

时间:2016-10-10 11:33:40

标签: python-3.x selenium

我遇到了问题。程序必须获取IP地址frome txt文件并为每个ip提供。检查记录是否成功。 我不知道为什么在这个简单的代码中我遇到了问题。

from termcolor import colored
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException


myfile = [p.rstrip() for p in open('test.txt')]

for ip in myfile:

    driver = webdriver.Chrome('./lib/chromedriver.exe')
    driver.get("http://admin:password@" + ip)
    try:
        element = WebDriverWait(driver, 20).until(
            EC.presence_of_element_located(By.XPATH, ".//*/tbody/tr/td/table/tbody/tr[2]/td[2]")
        )
    except TimeoutException:
        print(colored(ip + " except timeout error", "red"))

    else:
        print(colored(ip + " is OK", "green"))
    finally:
        driver.quit()

错误:

Traceback (most recent call last):
   File "E:/GS/Py/wifiproject/dwl3200.py", line 21, in <module>
   EC.presence_of_element_located(By.XPATH, ".//*/tbody/tr/td/table/tbody/tr[2]/td[2]")
    TypeError: __init__() takes 2 positional arguments but 3 were given

1 个答案:

答案 0 :(得分:1)

尝试使用

EC.presence_of_element_located((By.XPATH, ".//*/tbody/tr/td/table/tbody/tr[2]/td[2]")))

你应该提供By.locator和实际定位器表达式作为参数元组,但不能作为两个单独的参数