python - 尝试使用 selenium python 脚本登录谷歌
问题描述
作为我的第一步,我正在使用 selenium 打开并登录 Google 帐户。我已经成功打开并填写了电子邮件回复,尽管在提交时我收到了错误
“此浏览器或应用程序可能不安全。了解更多尝试使用其他浏览器。如果您已经在使用受支持的浏览器,则可以刷新屏幕并再次尝试登录。” 来自谷歌。
有没有办法解决这个问题?下面是我的代码。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://accounts.google.com/")
print(driver.title)
search = driver.find_element_by_name("identifier")
search.send_keys("email goes here")
search.send_keys(Keys.RETURN)
解决方案
遇到了同样的问题,我在 GitHub 中找到了这个线程。
对我有用的解决方案是使用这个驱动程序:undetected_chromedriver
而不是正常的ChromeDriver
.
import undetected_chromedriver.v2 as uc
chrome_options = uc.ChromeOptions()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-popup-blocking")
chrome_options.add_argument("--profile-directory=Default")
chrome_options.add_argument("--disable-plugins-discovery")
chrome_options.add_argument("--incognito")
chrome_options.add_argument("user_agent=DN")
self.browser = uc.Chrome(options=chrome_options)
self.browser.delete_all_cookies()
# example of loggin in to youtube without getting that issue
self.browser.get('http://youtube.com')
login_button_init = self.browser.find_element_by_xpath("//a[@aria-label='Sign in']")
login_button_init.click()
# locate the login button
login_button = self.browser.find_element_by_xpath("//paper-button[@aria-label='Sign in']")
login_button.click()
# get email and set to email input box
email = self.browser.find_element_by_id("identifierId")
myemail = os.environ.get('YOUTUBE_EMAIL')
email.send_keys(myemail)
# click next button
email_next_button = self.browser.find_element_by_id("identifierNext")
email_next_button.click()
# get password and set to password input box
password = self.browser.find_element_by_name("password")
mypassword = os.environ.get('YOUTUBE_PASSWORD')
password.send_keys(mypassword)
sleep(2)
# click next button to log in
pass_next_button = self.browser.find_element_by_id("passwordNext")
pass_next_button.click()
推荐阅读
- ios - 如何在swift中执行条件segue操作?
- r - 如何在R中循环和列出随机样本
- android - 如何以编程方式获取 Android 上 /data/user/0/com.companyname/files 位置上方的文件夹?
- java - 如何检索按 ID 分组的所有实体?
- javascript - 您如何在 discord.js 中使用带有 awaitMessages 的 set Interval,我想让它按顺序排列
- c++ - 如何判断c++向量中的值类型(int或double)?
- react-native - FacebookAds:新应用不再支持广告请求中的 SDK 版本
- reactjs - 使用 npm 的 MQTT 包在 React 中操作不安全
- java - 从客户端到服务的简单 AJAX 调用
- rust - Rust 的流失,迭代器被丢弃......“删除任何剩余的元素”