0

I'm trying to automate a duolingo login with Selenium with the code posted below. While everything seems to work as expected at first, I always get an "Wrong password" message on the website after the login button is clicked. I have checked the password time and time again and even changed it to one without special characters, but still the login fails. I have seen in other examples that there is sometimes an additional password input field, however I cannot find one while inspecting the html.

What could I be missing ?

(Side note: I'm also open to a completely different solution without a webdriver since I really only want to get to the duolingo.com/learn page to scrape some data, but as of yet I haven't found an alternative way to login)

The code used:

from selenium import webdriver
from time import sleep

url = "https://www.duolingo.com/"


def login():
    driver = webdriver.Chrome()
    driver.get(url)

    sleep(2)

    hve_acnt_btn = driver.find_element_by_xpath("/html/body/div/div/div/span[1]/div/div[1]/div[2]/div/div[2]/a")
    hve_acnt_btn.click()

    sleep(2)

    email_input = driver.find_element_by_xpath("/html/body/div[1]/div[3]/div[2]/form/div[1]/div/label[1]/div/input")
    email_input.send_keys("email@email.com")

    sleep(2)

    pwd_input = driver.find_element_by_css_selector("input[type=password]")
    pwd_input.clear()
    pwd_input.send_keys("password")

    sleep(2)

    login_btn = driver.find_element_by_xpath("/html/body/div[1]/div[3]/div[2]/form/div[1]/button")
    login_btn.click()

    sleep(5)

login() 

I couldn't post the website's html because of the character limit, so here is the link to the duolingo page: Duolingo

NoobyJane
  • 3
  • 3

1 Answers1

1

Switch to Firefox or a browser which does not tell the page that you are visiting it automated. See my earlier answer for a very similar issue here: https://stackoverflow.com/a/57778034/8375783

Long story short: When you start Chrome it will run with navigator.webdriver=true. You can check it in console. Pages can detect that flag and block login or other actions, hence the invalid login. This is a read-only flag set by the browser during startup.

With Chrome I couldn't log in to Duolingo either. After I switched the driver to Firefox, the very same code just worked.

Also if I may recommend, try to use Xpath with attributes.

Instead of this:

hve_acnt_btn = driver.find_element_by_xpath("/html/body/div/div/div/span[1]/div/div[1]/div[2]/div/div[2]/a")

You can use:

hve_acnt_btn = driver.find_element_by_xpath('//*[@data-test="have-account"]')

Same goes for:

email_input = driver.find_element_by_xpath("/html/body/div[1]/div[3]/div[2]/form/div[1]/div/label[1]/div/input")

vs:

email_input = driver.find_element_by_xpath('//input[@data-test="email-input"]')
Trapli
  • 1,517
  • 2
  • 13
  • 19