如何使用Python中的Selenium Webdriver最佳方式登录Gmail?

huangapple go评论60阅读模式
英文:

What is the best way to log into Gmail using Selenium Webdriver in Python?

问题

我用Python的Selenium库与Chrome浏览器登录Gmail,但登录时遇到了问题,参考图片 查看图片

这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

EMAIL = "your text"

PASS = "your text"

chrom_path = "C:\\development\\chromedriver_win32"

driver = webdriver.Chrome(executable_path=chrom_path)

driver.get("https://mail.google.com/mail/u/0/?tab=rm&ogbl#inbox")

email_box = driver.find_element(By.XPATH, '//*[@id="identifierId"]')

email_box.send_keys(EMAIL)

email_box.send_keys(Keys.ENTER)

time.sleep(5)

pass_box = driver.find_element(By.XPATH, '//*[@id="password"]/div[1]/div/div[1]/input')

pass_box.send_keys(PASS)

pass_box.send_keys(Keys.ENTER)

time.sleep(5)
英文:

I use selenium with python to login to my Gmail with chrome browser, But there was a problem while logging in look to photo .
look to it

this is my code :

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

EMAIL="your text"

PASS = "your text"

chrom_path ="C:\\development\\chromedriver_win32"

driver = webdriver.Chrome(executable_path=chrom_path)

driver.get("https://mail.google.com/mail/u/0/?tab=rm&ogbl#inbox")

email_box= driver.find_element(By.XPATH, '//\*\[@id="identifierId"\]')

email_box.send_keys(EMAIL)

email_box.send_keys(Keys.ENTER)

time.sleep(5)

pass_box= driver.find_element(By.XPATH, '//\*\[@id="password"\]/div\[1\]/div/div\[1\]/input')

pass_box.send_keys(PASS)

pass_box.send_keys(Keys.ENTER)

time.sleep(5)

答案1

得分: 1

你遇到这个错误的原因是因为GMAIL识别了机器人(您的脚本)并阻止您登录。

为了解决这个问题,您可以使用一个名为undetected-chromdriver的Python库。该库是为相同的目的而开发的。您只需安装此库并在您的代码中导入它,如下所示:

import undetected_chromedriver as uc

# driver = webdriver.Chrome()
driver = uc.Chrome()

有关undetected-chromedriver的更多信息,请参考以下链接:

https://pypi.org/project/undetected-chromedriver/2.1.1/

https://www.zenrows.com/blog/undetected-chromedriver#what-is

https://www.zenrows.com/blog/undetected-chromedriver#conclusion

英文:

The reason you get this error is because GMAIL identifies the bot(your script) and blocks you from logging in.

To overcome this issue, you can use a python library known as undetected-chromdriver. This library has been developed for the same purpose. You just have to install this library and import it in your code as use it as below:

import undetected_chromedriver as uc

# driver = webdriver.Chrome()
driver = uc.Chrome()

For more info about undetected-chromedriver refer the below:

https://pypi.org/project/undetected-chromedriver/2.1.1/

https://www.zenrows.com/blog/undetected-chromedriver#what-is

https://www.zenrows.com/blog/undetected-chromedriver#conclusion

答案2

得分: 0

Gmail封锁了您,因为他们的网站检测到了一个Selenium机器人。

您可以通过在“uc”模式下使用SeleniumBase来绕过机器人检测。

首先执行 pip install seleniumbase。然后使用 python 运行以下脚本:

from seleniumbase import SB

with SB(uc=True) as sb:
    sb.open("https://www.google.com/gmail/about/")
    sb.click('a[data-action="sign in"]')
    sb.type('input[type="email"]', "test123@gmail.com")
    sb.click('button:contains("Next")')
    sb.sleep(5)
    # sb.type('input[type="password"]', PASSWORD)
    # sb.click('button:contains("Next")')

(要访问脚本中的原始 driver,请使用 sb.driver)

英文:

Gmail blocked you because their site detected a selenium bot.

You can bypass bot-detection by using SeleniumBase in uc mode.

First pip install seleniumbase. Then run the following script with python:

from seleniumbase import SB

with SB(uc=True) as sb:
    sb.open("https://www.google.com/gmail/about/")
    sb.click('a[data-action="sign in"]')
    sb.type('input[type="email"]', "test123@gmail.com")
    sb.click('button:contains("Next")')
    sb.sleep(5)
    # sb.type('input[type="password"]', PASSWORD)
    # sb.click('button:contains("Next")')

(To access the raw driver from the script, use sb.driver)

huangapple
  • 本文由 发表于 2023年5月21日 23:01:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300532.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定