0

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()
pppery
  • 3,731
  • 22
  • 33
  • 46
A-Mehrabi
  • 66
  • 1
  • 7

1 Answers1

3

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

Furkan Ozalp
  • 334
  • 1
  • 4
  • 7
  • Thank you very much for taking the time to answer my question i installed undetected-chromedriver but browser be closed after executing above code. can you tell me why?!! – A-Mehrabi Apr 09 '22 at 20:55
  • Because you don't have any code after that. Selenium works like another python codes, it handle your code then shut it down. If you want to keep running your code, you need to set it. Like use while etc.. Example: https://stackoverflow.com/questions/57417904/how-to-automatically-keep-a-script-running – Furkan Ozalp Apr 09 '22 at 21:09