How to login to `moz.com` with selenium?

165 views Asked by At

I can log in to moz.com without any problems with my default Windows browser (Chrome), but I can not log in with the Chrome driver. Is there any problem with the following code snippet?

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
# login to MOZ
username = "my email"
password = "my pass"
# head to github login page
driver.get("https://moz.com/login")
# find username/email field and send the username itself to the input field
driver.find_element(By.NAME,"email").send_keys(username)
# find password input field and insert password as well
driver.find_element(By.NAME,"password").send_keys(password)
# click login button
driver.find_element(By.XPATH,'//input[@class="forge-btn forge-btn-yellow"]').click()
1

There are 1 answers

2
Furkan Ozalp On

The website has security measures. You can add extra options to your webdriver to avoid that or you can use undetected chrome.

First, install undetected chrome:

pip install undetected-chromedriver

Then you can use it as follows:

from selenium.webdriver.common.by import By
#import undetected chrome
import undetected_chromedriver as uc 

#uc driver
driver = uc.Chrome(use_subprocess=True) #use_subprocess=True Otherwise it raises __name__='__main__' error.

#Your code..
username = "my email"
password = "my pass"
# head to github login page
driver.get("https://moz.com/login")
# find username/email field and send the username itself to the input field
driver.find_element(By.NAME,"email").send_keys(username)
# find password input field and insert password as well
driver.find_element(By.NAME,"password").send_keys(password)
# click login button
driver.find_element(By.XPATH,'//input[@class="forge-btn forge-btn-yellow"]').click()

For more, you can see: https://github.com/ultrafunkamsterdam/undetected-chromedriver