【问题标题】:Selenium TypeError: 'WebElement' object is not callableSelenium TypeError:“WebElement”对象不可调用
【发布时间】:2020-11-09 07:47:23
【问题描述】:

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

    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

我是初学者,请帮助我

【问题讨论】:

  • 您没有正确调用.until()。它不采用普通元素作为参数。

标签: python selenium typeerror


【解决方案1】:

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

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

试试这个:

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

Explicit Waits

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 2020-03-03
    • 2022-12-12
    • 2023-01-15
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    相关资源
    最近更新 更多