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

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

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

问题

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

这是我的代码:

  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.common.keys import Keys
  4. import time
  5. EMAIL = "your text"
  6. PASS = "your text"
  7. chrom_path = "C:\\development\\chromedriver_win32"
  8. driver = webdriver.Chrome(executable_path=chrom_path)
  9. driver.get("https://mail.google.com/mail/u/0/?tab=rm&ogbl#inbox")
  10. email_box = driver.find_element(By.XPATH, '//*[@id="identifierId"]')
  11. email_box.send_keys(EMAIL)
  12. email_box.send_keys(Keys.ENTER)
  13. time.sleep(5)
  14. pass_box = driver.find_element(By.XPATH, '//*[@id="password"]/div[1]/div/div[1]/input')
  15. pass_box.send_keys(PASS)
  16. pass_box.send_keys(Keys.ENTER)
  17. 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 :

  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.common.keys import Keys
  4. import time
  5. EMAIL="your text"
  6. PASS = "your text"
  7. chrom_path ="C:\\development\\chromedriver_win32"
  8. driver = webdriver.Chrome(executable_path=chrom_path)
  9. driver.get("https://mail.google.com/mail/u/0/?tab=rm&ogbl#inbox")
  10. email_box= driver.find_element(By.XPATH, '//\*\[@id="identifierId"\]')
  11. email_box.send_keys(EMAIL)
  12. email_box.send_keys(Keys.ENTER)
  13. time.sleep(5)
  14. pass_box= driver.find_element(By.XPATH, '//\*\[@id="password"\]/div\[1\]/div/div\[1\]/input')
  15. pass_box.send_keys(PASS)
  16. pass_box.send_keys(Keys.ENTER)
  17. time.sleep(5)

答案1

得分: 1

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

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

  1. import undetected_chromedriver as uc
  2. # driver = webdriver.Chrome()
  3. 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:

  1. import undetected_chromedriver as uc
  2. # driver = webdriver.Chrome()
  3. 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 运行以下脚本:

  1. from seleniumbase import SB
  2. with SB(uc=True) as sb:
  3. sb.open("https://www.google.com/gmail/about/")
  4. sb.click('a[data-action="sign in"]')
  5. sb.type('input[type="email"]', "test123@gmail.com")
  6. sb.click('button:contains("Next")')
  7. sb.sleep(5)
  8. # sb.type('input[type="password"]', PASSWORD)
  9. # 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:

  1. from seleniumbase import SB
  2. with SB(uc=True) as sb:
  3. sb.open("https://www.google.com/gmail/about/")
  4. sb.click('a[data-action="sign in"]')
  5. sb.type('input[type="email"]', "test123@gmail.com")
  6. sb.click('button:contains("Next")')
  7. sb.sleep(5)
  8. # sb.type('input[type="password"]', PASSWORD)
  9. # 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:

确定