Long time reader, first time poster.
Using Selenium and python I am attempting to create a program that will upload the new hires at my company to an online database for Grainger.com. This database will allow our employees to purchase new boots on a yearly basis to replace old boots that they received the year before.
The issue I am running across is on the login page. I have some experience with Selenium and have not run into this problem before. When performing this process manually, it works just fine. The Selenium script I wrote will input my username and password, and will find and click on the "Sign In" button (It will not raise an exception), but the "Sign In" button/function that it is supposed to log me in, is either dead or not functional. Even when trying to click on the "Sign In" button in the Selenium Chrome browser, the button will not work.
Below is the function I wrote to handle the login process of my script.
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
chrome_driver_path = r"C:\Users\cyendes\OneDrive - Environmental Works\Work Programs\Selenium Project\chromedriver.exe"
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
driver = webdriver.Chrome(executable_path = chrome_driver_path, chrome_options= options)
grainger_login_page = "https://www.grainger.com/myaccount/signin"
def nav_thru_grainger_login_page(webpage):
"""This function will take us to grainger where we can start putting in new hire information"""
driver.get(webpage)
time.sleep(2)
#Sending the username
driver.find_element(By.XPATH, "//*[@id='signin_username'] ").send_keys('[email protected]')
time.sleep(1)
#Sending the password
driver.find_element(By.XPATH, "//*[@id='signin_password']").send_keys('JohnnyDeeny420!')
time.sleep(1)
#This is where the button should be clicked
button = driver.find_element(By.XPATH, "//*[@id='sign-in-form']/button")
ActionChains(driver).move_to_element(button).click().perform()
time.sleep(2)
driver.find_element(By.CSS_SELECTOR, "input[class='btn btnPrimary']").click()
nav_thru_grainger_login_page(grainger_login_page)
The weird part of this question is that if I take out the parts of the function where the user and password are sent to their respective inputs, the button will be clicked and the website will tell me that I needed to put in the username and password information before proceeding. But when running the function as written the button will not work when clicked on.
I have also tried the traditional click() method, submit() method, execute_script method, send_keys "RETURN"/"ENTER", and the ActionChains method (see the code). All of these methods will click the "Sign In" button without the username and password inputs filled out, but will not cause the button to act when username/password inputs are filled with information.
The HTML code I am working with from grainger.com/myaccount/signin is here: Grainger HTML
Any help is appreciated and thank you.
CodePudding user response:
I was able to make it work using this SeleniumBase script:
First pip install seleniumbase
, then run with python
:
from seleniumbase import SB
with SB(uc=True, slow=True, incognito=True) as sb:
sb.open("https://www.grainger.com/myaccount/signin")
sb.type("input#signin_username", "[email protected]")
sb.type("input#signin_password", "JohnnyDeeny420!")
sb.click('button[data-test="signin-submit-button"]')
sb.sleep(1)
sb.type('input[name="searchQuery"]', "headlamp\n")
sb.sleep(1)
sb.assert_element('h2[data-testid="category-tabs-title"]')
sb.assert_text('Showing results for "headlamp"')
sb.click('a[title="Safety-Rated Headlamps"]')
sb.assert_element('img[alt="Safety-Rated Headlamps image"]')
sb.sleep(5)