英文:
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
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论