Skip to content
Advertisement

Login Automation Using Selenium Not Working Properly

I have built a login Automator using Selenium, and the code executes without errors but the script doesn’t login. The page is stuck at login page, email and password are entered, but login is not completed. enter image description here

I have tried 2 ways to login:

  1. By clicking on Login through Click ()
e = self.driver.find_element(By.XPATH, "//div[text()='Login']")
e.click()
  1. Using Enter in password area
password_element.send_keys(Keys.ENTER)

But neither of them logs me in, even though I can see the button being clicked, and name and password being entered. I also tried adding a wait time, but the problem is not solved. What an I doing wrong?

Here is the code:

import pandas as pd
from selenium import webdriver
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

class QuoraScraper:

    def __init__(self):
        self.driver = ''
        self.dataframe = ''
        self.credentials = {
            'email': 'email',
            'password': 'password'
        }
        self.questions = []
        self.answers = []


    def start_driver(self):
        options = Options()
        options.binary_location = r'C:Program FilesMozilla Firefoxfirefox.exe'
        self.driver = webdriver.Firefox(options=options)

    def close_driver(self):
        self.driver.close()

    def open_url(self, url):
        self.driver.get(url)

    def initialize_columns(self, columns):
        self.dataframe = pd.DataFrame(columns=columns)

    def set_credentials(self, email, password):
        self.credentials['email'] = email
        self.credentials['password'] = password

    def login(self):
        self.open_url('https://www.quora.com/')

        if (self.credentials['email'] and self.credentials['password']):

            email_element = self.driver.find_element(By.ID, 'email')
            password_element = self.driver.find_element(By.ID, 'password')
            email_element.send_keys(self.credentials['email'])
            password_element.send_keys(self.credentials['password'])
            # I tried adding a wait time but the script is not successful either way
            #self.driver.maximize_window()  # For maximizing window
            #self.driver.implicitly_wait(20)  # gives an implicit wait for 20 seconds
            # I tried clcking on Login through both Click and Enter but neither of them logs me in, even though I can see the button clicking
            password_element.send_keys(Keys.ENTER)
            e = self.driver.find_element(By.XPATH, "//div[text()='Login']")
            e.click()
        else:
            print('Credentials not set. Error')


if __name__ == "__main__":
    scraper = QuoraScraper()
    scraper.start_driver()
    email = "email"
    password = "password"
    scraper.set_credentials(email, password)
    scraper.login()

UPDATE 2: I get a login popup window after the email and password have been correctly entered and i try to close it by finding the xpath of the X button like this: POPUP IMAGE

cross = self.driver.find_element(By.XPATH, '//*[@id="close"]')
cross.click()

But the element cannot be located:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //*[@id=”close”]

Advertisement

Answer

I typed this //div[text()='Login'] into source code of quora site to check if this is the correct xpath for the login button, but instead it highlighted the Login text on top of the email, this is the correct xpath for the login button :

login_btn =  driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/div/div/div/div/div/div[2]/div[2]/div[4]/button')
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement