【问题标题】:Selenium webdriverwait: __init__() takes exactly 2 arguments (3 given)Selenium webdriverwait:__init__() 正好需要 2 个参数(3 个给定)
【发布时间】:2016-09-22 10:11:31
【问题描述】:

给出的错误为

Traceback (most recent call last):
  File "p3.py", line 21, in <module>
    WebDriverWait(driver, timex).until(EC.presence_of_element_located(by, element))
TypeError: __init__() takes exactly 2 arguments (3 given)

我没用过__init__()为什么会出现这个错误?

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

chrome_path=r"C:\Users\Bhanwar\Desktop\New folder (2)\chromedriver.exe"
driver =webdriver.Chrome(chrome_path)
driver.get("https://priceraja.com/mobile/pricelist/samsung-mobile-price-list-in-india")
#driver.implicitly_wait(10)
i=0
timex = 5
by = By.ID
hook = "product-itmes-"  # The id of one item, they seems to be this plus 
                         # the number item that they are
button = '.loadmore'
while i<3:
    element_number = 25*i
    element=hook+str(element_number)# It looks like there are 25 items added each time, and starts at 25
    WebDriverWait(driver, timex).until(EC.presence_of_element_located(by, element))
    driver.find_element_by_css_selector(button).click()
    time.sleep(5)  # Makes the page wait for the element to change
    i+=1

【问题讨论】:

  • 请不要手动输入回溯或重新格式化它们。例如,我怀疑实际的错误可能是 __init__() 而不是 init()
  • 需要解决方案
  • 然后确保您的报告准确。这个__init__方法引起的;这是创建实例时调用的方法。 presence_of_element_located() 是一个带有__init__ 方法的类,它只需要一个locator,而不是两个参数。
  • 没错,你没有使用init(),但是你调用的类,使用。确保在第 21 行传递的参数不超过需要的参数。

标签: python selenium selenium-chromedriver


【解决方案1】:

presence_of_element_located() 只接受一个参数,一个定位器,它是一个元组。您忘记在调用中添加元组所需的 (...) 括号:

WebDriverWait(driver, timex).until(
    EC.presence_of_element_located((by, element)))
#          these make this a tuple ^ and       ^

【讨论】:

  • 我们应该回答有印刷错误的问题还是关闭它们?
  • @PadraicCunningham:不确定这是否是印刷问题。如果您认为是,请随时投票关闭它。
  • 我做到了,根据socvr.org 中的一些人的说法,我们只是应该关闭而不回答,以便更容易删除它们。你是一个模组,所以这就是我问的原因。
  • 在我意识到 what 对象抛出异常及其原因之前,我必须做一些阅读,然后再了解 selenium 位置的工作原理。因此,在我了解您应该如何将位置传递给 Selenium 对象之前,这对我来说并不是一个明显的错字。
猜你喜欢
  • 2012-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多