首页 > 解决方案 > 尝试使用 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)

标签: pythonseleniumgoogle-chromeselenium-webdriver

解决方案


遇到了同样的问题,我在 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()

推荐阅读