一直在尝试使用 Selenium 来抓取 Steam 游戏相关网站,但不知道如何让 Selenium 找到 Steam Guard 文本框和“Enter”按钮。相反,我得到的错误是
NameError: name 'authbox' is not defined
这意味着 Selenium 无法将 authbox 变量分配给 html 元素。
在下面的代码中,您需要输入一些有效的 Steam 登录信息才能显示 Steam Guard 提示:
import time
import requests, bs4
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
# Steam username
username = 'usernamehere'
# Steam password
password = 'passwordhere'
authcode = "FAKE"
delay = 1
#Using selenium to log in and then get the page
browser = webdriver.Chrome()
browser.get('https://scrap.tf/buy/hats')
#Log into steam then proceed to new page
userbox = browser.find_element_by_name("username")
userbox.send_keys(username)
passbox = browser.find_element_by_name("password")
passbox.send_keys(password)
signin = browser.find_element_by_id("imageLogin")
#signin.click()
time.sleep(delay)
authbox.send_keys(authcode)
submitBtn = browser.find_element_by_class("auth_button leftbtn")
submitBtn.click()
TL;DR:按下“登录”后,如何让 selenium 识别 Steam Guard 提示文本框?如果代码正常工作,应该在文本框中输入“FAKE”
最佳答案
正如 @Prophet 所提到的, authbox
未定义,因此您无法执行 send_keys
。
相反,您可以尝试以下代码:
signin = browser.find_element_by_id("imageLogin")
signin.click()
wait = WebDriverWait(browser, 10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#authcode"))).send_keys(authcode)
wait.until(EC.element_to_be_clickable((By.XPATH, "(//div[text()='Submit']/..)[6]"))).click()
更新1:
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://scrap.tf/buy/hats")
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, "steamAccountName"))).send_keys('username')
wait.until(EC.element_to_be_clickable((By.ID, "steamPassword"))).send_keys('password')
wait.until(EC.element_to_be_clickable((By.ID, "imageLogin"))).click()
wait.until(EC.element_to_be_clickable((By.ID, "authcode"))).send_keys('12345')
wait.until(EC.element_to_be_clickable((By.XPATH, "(//div[text()='Submit'])[6]"))).click()
导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
但是,会输入12345
并点击提交
,但是应用程序端会出现一些错误。不幸的是我们对此无能为力。因为它需要发送到您注册的手机号码的安全代码。
关于python-3.x - 如何使用 Selenium 在 Steam Guard 提示符中输入文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68335357/