python - 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
我是初学者,请帮助我
解决方案
TypeError: init () 接受 2 个位置参数,但给出了 3 个
上面的错误很清楚,出现是因为你需要 3 个位置参数。
尝试这个:
element = WebDriverWait(driver, 60).until(ec.presence_of_element_located((By.ID, "ap_password")))
推荐阅读
- javascript - dc.js - 热图的矩形画笔?
- python - Python Pandas - 向每一行添加新的总和
- android - Libgdx 和 Google Play 计费
- css - 如何降低顶部菜单栏的高度
- java - Maven 程序集插件目标“jar-with-dependencies”不包含来自子 pom 的 jar 文件
- sqlite - 删除 SQLite 中的重复组
- javascript - 为远程文件(视频、照片等)创建 blob url 下载链接
- rust - 为什么“rev”在 Cargo 中不起作用?
- django - Django 如何仅将 example.com 重定向到 https 并将 *.example.com 重定向到 http?
- java - 使用同步或不低于为所有线程生成相同哈希码的代码。如何?