__init__() 接受 2 个位置参数,但给出了 3 个试图使用 presence_of_element_located() 等待元素

__init__() takes 2 positional arguments but 3 were given trying to wait for an element using presence_of_element_located()

日志向我抛出这个:

element.until(EC.presence_of_element_located(By.XPATH("//*[@id='menu-item-9145']/a'")))
TypeError: 'str' object is not callable

代码试验:

class Descaro:
    def __init__(self, driver):
        self.driver = driver

    def Descaro(self):
        time.sleep(3)
        self.driver.find_element_by_xpath("//*[@id='splashModal']/a[1]").click()
        print("deberia estar en la pagina de fondo")
        element = WebDriverWait(self.driver, 10)
        element.until(EC.presence_of_element_located(By.XPATH("//*[@id='menu-item-9145']/a'")))
        element.click()

我已经试过了:

element.until(EC.presence_of_element_located(By.XPATH, '//*[@id="menu-item-9145"]/a''))) 

但也不起作用,因为:

__init__() takes 2 positional arguments but 3 were given

您需要处理几件事:

  • presence_of_element_located() 应该在 tuple 中调用,因为它不是 函数 而是 class,其中初始化程序只需要 1 参数,超出 implicit self
  • doesn't ensures that the element is interactable. Instead you need to use

解决方案

你需要诱导WebDriverWait for the element_to_be_clickable() and you can use the following :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='menu-item-9145']/a"))).click()

注意:您必须添加以下导入:

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

参考

您可以在以下位置找到一些相关的详细讨论: