0

I have been able to get past the pop up in my code but I am not able to then type in the email and password for the account via sending the keys and then hit the login button.

Here is the code I am using and the site I am trying to login to. I am VERY new so any response please try to explain so I am able to learn from my mistakes.

Thanks so much in advance!

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time
from time import sleep
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())

PATH = "C:\Program Files\chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.implicitly_wait(5)

driver.get("https://www.finewineandgoodspirits.com/webapp/wcs/stores/servlet/LogonForm?langId=-1&storeId=10051&catalogId=10051")


main_page = driver.current_window_handle
sleep(1)
driver.find_element_by_xpath('//*[contains(text(), "at least 21 years old.")]').click()
sleep(1)
driver.find_element_by_xpath('//*[contains(text(), "at least 21 years old.")]/span').click()

for handle in driver.window_handles:
    if handle != main_page:
        login_page = handle
driver.switch_to.window(login_page)
driver.find_element_by_xpath('//*[contains(text(), "at least 21 years old.")]').click()
driver.switch_to.window(main_page)

WebDriverWait(driver,1)

fwgsUsername = credentials.login['candmidlik@yahoo.com']
fwgsPassword = credentials.login['Password2']
emailFieldID     = "logonId"
passFieldID      = "logonPassword"
loginButtonXpath = "//input[@id = 'loginButton']"

emailFieldElement   = WebDriverWait(driver,1).until(lambda driver: driver.find_element_by_name(emailFieldID))
emailFieldElement.clear()
emailFieldElement.send_keys(fwgsUsername)

passFieldElement    = WebDriverWait(driver,1).until(lambda     driver: driver.find_element_by_name(passFieldID))
passFieldElement.clear()
passFieldElement.send_keys(fwgsPassword)

logInButtonElement  = WebDriverWait(driver,1).until(lambda driver: driver.find_element_by_xpath(loginButtonXpath))
logInButtonElement.click()
Candcrush
  • 1
  • 2

2 Answers2

0

To send a character sequence within the E-mail field you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get("https://www.finewineandgoodspirits.com/webapp/wcs/stores/servlet/LogonForm?langId=-1&storeId=10051&catalogId=10051")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='YES']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.emailInput_myacc[name='logonId']"))).send_keys("Candcrush@Candcrush.Candcrush")
    driver.find_element(By.CSS_SELECTOR, "input.emailInput_myacc[name='logonPassword']").send_keys("Candcrush")
    driver.find_element(By.CSS_SELECTOR, "a#loginButton").click()
    
  • Using XPATH:

    driver.get("https://www.finewineandgoodspirits.com/webapp/wcs/stores/servlet/LogonForm?langId=-1&storeId=10051&catalogId=10051")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='YES']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='emailInput_myacc' and @name='logonId']"))).send_keys("Candcrush@Candcrush.Candcrush")
    driver.find_element(By.XPATH, "//input[@class='emailInput_myacc' and @name='logonPassword']").send_keys("Candcrush")
    driver.find_element(By.XPATH, "//a[@id='loginButton']").click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

CandyCrush

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • This worked! Thank you so much. Which is better use css selector or xpath? – Candcrush Dec 06 '21 at 19:44
  • @Candcrush Of coarse [_**CssSelector**_](https://stackoverflow.com/questions/51936193/why-should-i-ever-use-css-selectors-as-opposed-to-xpath-for-automated-testing/51942615#51942615) – undetected Selenium Dec 06 '21 at 19:55
0

You should swap over to webdriver wait for the yes since it's not switching windows. Also your xpath for the login was an a tag.

wait=WebDriverWait(driver, 60)
driver.get("https://www.finewineandgoodspirits.com/webapp/wcs/stores/servlet/LogonForm?langId=-1&storeId=10051&catalogId=10051")

wait.until(EC.element_to_be_clickable((By.XPATH,'//span[.="YES"]'))).click()


fwgsUsername = 'candmidlik@yahoo.com'
fwgsPassword = 'Icecream90'
emailFieldID     = "logonId"
passFieldID      = "logonPassword"
loginButtonXpath = "//a[@id = 'loginButton']"

emailFieldElement   = WebDriverWait(driver,1).until(lambda driver: driver.find_element_by_name(emailFieldID))
emailFieldElement.clear()
emailFieldElement.send_keys(fwgsUsername)

passFieldElement    = WebDriverWait(driver,1).until(lambda  driver: driver.find_element_by_name(passFieldID))
passFieldElement.clear()
passFieldElement.send_keys(fwgsPassword)

logInButtonElement  = WebDriverWait(driver,1).until(lambda driver: driver.find_element_by_xpath(loginButtonXpath))
logInButtonElement.click()

Imports:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32