首页 > 解决方案 > Selenium TypeError:“WebElement”对象不可调用

问题描述

我正在尝试在亚马逊上自动输入礼品卡,我制作了这段代码

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from webdriver_manager.chrome import ChromeDriverManager
    from selenium.webdriver.support.wait import WebDriverWait
    import time
    
    options = webdriver.ChromeOptions()
    options.add_argument("--incognito")
    driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
    
    driver.get("https://www.amazon.com/ap/signin?clientContext=131-4641921-7497613&openid.return_to=https%3A%2F%2Fwww.amazon.com%2Fgc%2Fredeem%2F&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=amzn_gcfront_v2_us&openid.mode=checkid_setup&marketPlaceId=ATVPDKIKX0DER&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&pageId=amzn_gcfront_v2_us&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.pape.max_auth_age=3600&siteState=clientContext%3D144-1807701-9645427%2CsourceUrl%3Dhttps%253A%252F%252Fwww.amazon.com%252Fgc%252Fredeem%252F%2Csignature%3DduV3UfJbYbHeREFcPDmzxQt6fpIj3D")
    usr = driver.find_element_by_id("ap_email")
    usr.clear()
    usr.send_keys("username/email")
    pwd = driver.find_element_by_id("ap_password")
    pwd.clear()
    pwd.send_keys("pass")
    pwd.submit()
    
    #after email confirmation

    element = WebDriverWait(driver, 60).until(driver.find_element_by_id("ap_password"))
    pwd = driver.find_element_by_id("ap_password")
    pwd.clear()
    pwd.send_keys("pass")
    pwd.submit()
    
    for i in range(1000):
        driver.get("https://www.amazon.com/gc/redeem/")
        elem = driver.find_element_by_id("gc-redemption-input")
        elem.clear()
        code_file = open("file directory", "r")
        code = code_file.readlines()
        code_file.close()
        elem.send_keys(code[i])
        elem.submit()

但我收到了这个错误:

Traceback (most recent call last):
  File "AutoSubmitGiftcardAmazon.py", line 20, in <module>
    element = WebDriverWait(driver, 60).until(driver.find_element_by_id("ap_password"))
  File "/usr/lib/python3/dist-packages/selenium/webdriver/support/wait.py", line 77, in until
    value = method(self._driver)
TypeError: 'WebElement' object is not callable

我希望此代码等待我确认我的电子邮件,然后自动填写密码
请帮助

编辑:
我改变了

element = WebDriverWait(driver, 60).until(driver.find_element_by_id("ap_password"))

element = WebDriverWait(driver, 60).until(ec.presence_of_element_located(By.ID, "ap_password"))

我已经进口了

from selenium.webdriver.common.by import By
import selenium.webdriver.support.expected_conditions as ec

我得到了这个错误:

Traceback (most recent call last):
  File "AutoSubmitGiftcardAmazon.py", line 22, in <module>
    element = WebDriverWait(driver, 60).until(ec.presence_of_element_located(By.ID, "ap_password"))
TypeError: __init__() takes 2 positional arguments but 3 were given

我是初学者,请帮助我

标签: pythonseleniumtypeerror

解决方案


TypeError: init () 接受 2 个位置参数,但给出了 3 个

上面的错误很清楚,出现是因为你需要 3 个位置参数。

尝试这个:

element = WebDriverWait(driver, 60).until(ec.presence_of_element_located((By.ID, "ap_password")))

显式等待


推荐阅读