从今天起,用户无法在新配置文件中使用selenium登录到Google帐户。我发现,谷歌甚至在尝试使用stackauth时也会阻止这个过程(拒绝?((在更新到v90后体验到了这一点(
这是我之前发布的使用OAuth登录谷歌的答案,直到最近还在使用
简而言之,您将通过stackauth直接登录。
- 绕过限制的唯一方法是禁用安全应用程序访问或添加以下给定参数。(我不喜欢,因为我无法说服使用我的应用程序的用户(100多人(禁用它!(
options.add_argument('user-data-dir=C:/Users/{username}/path to data of browser/')
- 另一种唯一的登录方式是使用隐身将用户代理伪造为DN,这在这里提到过,效果非常好
- 我发现的主要缺点是,当自动化运行时,您无法打开另一个选项卡,否则,过程会中断。但这完全符合缺点
- 但我发现的缺点是,一旦你登录,你就无法完成你的工作,因为你访问的网站会限制你,并迫使你更新浏览器才能访问该网站(Google Meet在我的情况下(。另一方面,从理论上讲,可以用用户数据打开自动化,但在新窗口中。与除OAuth之外的其他公司相比,我觉得它是最佳的,因为这是最好的方法
谷歌还有其他绕过这些限制的最佳工作建议吗?
最后,我成功地绕过了Selenium中的Google安全限制,希望它也能帮助您。在此处共享整个代码。
简而言之:
- 您需要使用旧的/过时的用户代理并恢复
详细信息:
- 使用硒隐身伪造用户代理
- 在登录之前,首先将用户代理设置为
DN
- 然后,登录后,恢复正常。(不是真的,但chrome v>80(就是这样。
不需要保存用户数据,可以实现不太安全的应用程序访问什么都不用
这是我目前工作的代码片段,它很长!。(为了更好地理解,请附上评论(。
# Import required packages, modules etc.. Selenium is a must!
def login(username, password): # Logs in the user
driver.get("https://stackoverflow.com/users/login")
WebDriverWait(driver, 60).until(expected_conditions.presence_of_element_located(
(By.XPATH, '//*[@id="openid-buttons"]/button[1]'))).click()
try:
WebDriverWait(driver, 60).until(expected_conditions.presence_of_element_located(
(By.ID, "Email"))).send_keys(username) # Enters username
except TimeoutException:
del username
driver.quit()
WebDriverWait(driver, 60).until(expected_conditions.element_to_be_clickable(
(By.XPATH, "/html/body/div/div[2]/div[2]/div[1]/form/div/div/input"))).click() # Clicks NEXT
time.sleep(0.5)
try:
try:
WebDriverWait(driver, 60).until(expected_conditions.presence_of_element_located(
(By.ID, "password"))).send_keys(password) # Enters decoded Password
except TimeoutException:
driver.quit()
WebDriverWait(driver, 5).until(expected_conditions.element_to_be_clickable(
(By.ID, "submit"))).click() # Clicks on Sign-in
except TimeoutException or NoSuchElementException:
print('nUsername/Password seems to be incorrect, please re-checknand Re-Run the program.')
del username, password
driver.quit()
try:
WebDriverWait(driver, 60).until(lambda webpage: "https://stackoverflow.com/" in webpage.current_url)
print('nLogin Successful!n')
except TimeoutException:
print('nUsername/Password seems to be incorrect, please re-checknand Re-Run the program.')
del username, password
driver.quit()
USERNAME = input("User Name : ")
PASSWORD = white_password(prompt="Password : ") # A custom function for secured password input, explained at end.
# Expected and required arguments added here.
options = Options()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_experimental_option('excludeSwitches', ['enable-logging'])
# Assign drivers here.
stealth(driver,
user_agent='DN',
languages=["en-US", "en"],
vendor="Google Inc.",
platform="Win32",
webgl_vendor="Intel Inc.",
renderer="Intel Iris OpenGL Engine",
fix_hairline=True,
) # Before Login, using stealth
login(USERNAME, PASSWORD) # Call login function/method
stealth(driver,
user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36',
languages=["en-US", "en"],
vendor="Google Inc.",
platform="Win32",
webgl_vendor="Intel Inc.",
renderer="Intel Iris OpenGL Engine",
fix_hairline=True,
) # After logging in, revert back user agent to normal.
# Redirecting to Google Meet Web-Page
time.sleep(2)
driver.execute_script("window.open('https://the website that you wanto to go.')")
driver.switch_to.window(driver.window_handles[1]) # Redirecting to required from stackoverflow after logging in
driver.switch_to.window(driver.window_handles[0]) # This switches to stackoverflow website
driver.close() # This closes the stackoverflow website
driver.switch_to.window(driver.window_handles[0]) # Focuses on present website
单击此处了解white_password。
这样做:
- 安装此python模块
pip install selenium-stealth
- 将其添加到代码中:
from selenium_stealth import stealth
stealth(driver,
languages=["en-US", "en"],
vendor="Google Inc.",
platform="Win32",
webgl_vendor="Intel Inc.",
renderer="Intel Iris OpenGL Engine",
fix_hairline=True,
)
这对我有效。