How to input text into steam guard prompt with selenium?

Viewed 274

Been trying to use selenium to scrape steam-game related sites, but can't figure out how to get selenium to find the Steam Guard textbox and 'Enter' button. Instead I get the error that

NameError: name 'authbox' is not defined

Which means Selenium couldn't assign the authbox variable to a html element.

Code below, you'll need to input some valid steam login for the steam guard prompt to show up:

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: How do I get selenium to recognize the steam guard prompt textbox after pressing "login"? If the code works correctly, it should input "FAKE" into the textbox

2 Answers

You are trying to send text to some element with command authbox.send_keys(authcode) but authbox is not defined.
This is what Python trying to say you.
You probably forgot to define that variable by finding the appropriate element or something like this.

As mentioned by @Prophet, authbox is not defined so you cannnot do send_keys . Instead you can try the below code :

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()

Update 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()

Imports :

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

However, it will enter 12345 and click on submit, but there will be some error from application side. Unfortunately we cannot do anything about that. since it's expecting the security code which is sent to your registered mobile number.

Related