Skip to main content
 首页 » 编程设计

python之Selenium webdriverwait : __init__() takes exactly 2 arguments (3 given)

2025年05月04日41落叶

给出错误为

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 

请您参考如下方法:

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

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