python - 我正在使用 python 中的 selenium 并尝试在 chrome 中自动登录谷歌,但我遇到了问题
解决方案
使用此链接:
https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount?redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground&prompt=consent&response_type=code&client_id=407408718192.apps.googleusercontent.com&scope=email&access_type=offline&flowName=GeneralOAuthFlow
我不知道是什么问题,但谷歌有自己的安全协议,很难绕过它。
要使用它,您必须关闭两步验证和其他帐户安全性。就我而言,当我尝试登录时,前 5-6 次它要求我通过点击智能手机中的一个数字来验证(希望你知道那是什么),然后它对我来说工作正常。
现在,如果你想避免错误,你可以在 selenium中使用Explicit Wait 。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
WebDriverWait(driver, 100).until(ec.visibility_of_element_located((By.XPATH, '###')))
#Place the xpath of you element in place of ###
通过使用它,您可以让 selenium 等到元素加载然后继续。
如果问题继续出现,您可以使用第三方网站登录,其中包括堆栈溢出。
from selenium import webdriver
from time import sleep
username=raw_input("username: ")
password=raw_input("password: ")
driver=webdriver.Chrome('...') #path of your chrome driver
driver.get('https://stackoverflow.com/users/signup')
sleep(3)
driver.find_element_by_xpath('//*[@id="openid-buttons"]/button[1]').click()
driver.find_element_by_id('identifierId').send_keys(username)
driver.find_element_by_id('identifierNext').click()
sleep(3)
driver.find_element_by_name('password').send_keys(password)
driver.find_element_by_id('passwordNext').click()
sleep(2)
driver.get('https://mail.google.com/mail/u/0/#inbox')
现在,您可以获取任何 URL 并继续。
推荐阅读
- r - R-usmap:如何将标签更改为更小?
- html - 如何在 jQuery 数据表中将固定宽度设置为单列
- mysql - Knex.js - 如何使用池中的最大连接数
- haproxy - Haproxy SSL 终止:Layer4 连接问题,信息:“连接被拒绝”
- python - 如何在 Pandas 中使用默认索引作为数据透视表创建数据透视表?
- laravel - fetch() 函数在方法中不起作用
- sql - 如何在 SQL Server 中递归获取值?
- html - 如何在css中将一个容器放在另一个容器之上(而不是顶部)?
- r - R:一个单元格中有多个值,只选择其中一个?
- java - 使用转换器时无法从“convertToDatabaseColumn”抛出异常