Home > Back-end >  Unable to click on "Sign In" button with Selenium and Python
Unable to click on "Sign In" button with Selenium and Python

Time:12-15

I'm trying to automate a login with a user and password on a website, which is this one :

https://www.esselunga.it/area-utenti/applicationCheck?appName=esselungaEcommerce&daru=https://www.esselungaacasa.it:443/ecommerce/nav/auth/supermercato/home.html?&loginType=light

I managed to insert the user and the password trough Selenium, but I can't click on the "Accedi" button, which is the Italian word for "Sign In".

HTML:

<div>
   <button value="Accedi" type="submit">Accedi</button>
</div>

I tried the following python code:

1st try:

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/div[2]/div[2]/div[1]/form[1]/div/button"))).click()

2nd try:

button = driver.find_element(by=By.CSS_SELECTOR, value="Accedi")

3rd try:

button = driver.find_element(by=By.LINK_TEXT, value="Accedi")
button.click()

But without success. Can you please help me with this personal project?

CodePudding user response:

You can try:

# Needed libs
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

#Open the browser
driver = webdriver.Edge()
url = 'https://www.esselunga.it/area-utenti/applicationCheck?appName=esselungaEcommerce&daru=https://www.esselungaacasa.it:443/ecommerce/nav/auth/supermercato/home.html?&loginType=light'
driver.get(url)

user = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, f"gw_username")))
user.send_keys('[email protected]')
user = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, f"gw_password")))
user.send_keys('password')
login_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, f"//button[@value='Accedi']")))
login_button.click()
time.sleep(10)

But I see a big problem here...and the problem is that you have a captcha. With the hints I give you will solve the problem of clicking into the element, but in the moment you need to pass the captcha...that will be a different topic because captchas are created for avoiding bots (Selenium is a bot managing your browser).

For the problem with the captcha you can check this other answer: Captctha

I hope it helps!

CodePudding user response:

To click on Accedi first you have to click on the reCAPTCHA checkbox which is within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • Induce WebDriverWait for the reCAPTCHA element to be clickable.

  • Induce WebDriverWait for Accedi element to be clickable.

  • You can use the following locator strategies:

    • Using CSS_SELECTOR:

      driver.get('https://www.esselunga.it/area-utenti/applicationCheck?appName=esselungaEcommerce&daru=https://www.esselungaacasa.it:443/ecommerce/nav/auth/supermercato/home.html?&loginType=light')
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a#accettaTuttiCookie"))).click()
      driver.find_element(By.CSS_SELECTOR, "input#gw_username").send_keys("LeonardoVarè@stackoverflow.com")
      driver.find_element(By.CSS_SELECTOR, "input#gw_password").send_keys("LeonardoVarè")
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='reCAPTCHA']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span#recaptcha-anchor"))).click()
      driver.switch_to.default_content()
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[value='Accedi']"))).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
    
  • Related