Selenium Google 登录在自动化中被阻止
Selenium Google Login Blocked in Automation
从今天开始,用户无法在新配置文件中使用 selenium 登录 到 Google 帐户。我发现 Google 即使在尝试使用 stackauth 时也会阻止进程(拒绝?)。 (更新v90后体验)
这是我之前为使用 OAuth 登录 Google 发布的 ,直到最近才有效!
简而言之,您将通过 stackauth.
间接登录
- 我可以绕过限制的唯一方法是 禁用 Secure-App-Access 或添加下面给定的参数。(我不喜欢这样做我无法说服使用我的应用程序的用户(100+)禁用它!)
options.add_argument('user-data-dir=C:/Users/{username}/path to data of browser/')
- 另一种唯一的登录方式是使用隐身方式将用户代理伪造到提到的 DN here 并且效果很好。
- 我发现的主要缺点是,当自动化为 运行 时,您无法打开 另一个选项卡 ,否则,进程会中断。但这完美地解决了这个缺点。
- 但我发现的缺点是,一旦你登录,你就无法完成你的工作,因为你正在访问的网站会限制你并强制你更新浏览器才能访问该网站(Google Meet 在我的例子中)。
另一方面,从理论上讲,可以用用户数据打开自动化,但在新的 window 中。与 OAuth 以外的其他方法相比,我觉得它是最佳选择,因为它是实现它的最佳方式。
任何其他绕过这些限制的最佳工作建议Google?
最后,我成功绕过了 Selenium 中的 Google 安全限制,希望对您也有帮助。在这里分享整个代码。
简而言之:
- 您需要使用 old/outdated 用户代理并还原。
详细:
- 使用 selenium-stealth 伪造用户代理。
- 最初在登录前将用户代理设置为
DN
。
- 然后,登录后,恢复正常。(不是真的,但是 chrome v>80)
就是这样。
不需要保留用户数据,启用不太安全的应用程序访问,没有!
这是我目前可以使用的代码片段,虽然它很长!(包含评论以便更好地理解)。
# Import required packages, modules etc.. Selenium is a must!
def login(username, password): # Logs in the user
driver.get("https://whosebug.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-check\nand Re-Run the program.')
del username, password
driver.quit()
try:
WebDriverWait(driver, 60).until(lambda webpage: "https://whosebug.com/" in webpage.current_url)
print('\nLogin Successful!\n')
except TimeoutException:
print('\nUsername/Password seems to be incorrect, please re-check\nand 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 Whosebug after logging in
driver.switch_to.window(driver.window_handles[0]) # This switches to Whosebug website
driver.close() # This closes the Whosebug website
driver.switch_to.window(driver.window_handles[0]) # Focuses on present website
点击here了解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,
)
这对我有用。
从今天开始,用户无法在新配置文件中使用 selenium 登录 到 Google 帐户。我发现 Google 即使在尝试使用 stackauth 时也会阻止进程(拒绝?)。 (更新v90后体验)
这是我之前为使用 OAuth 登录 Google 发布的
简而言之,您将通过 stackauth.
- 我可以绕过限制的唯一方法是 禁用 Secure-App-Access 或添加下面给定的参数。(我不喜欢这样做我无法说服使用我的应用程序的用户(100+)禁用它!)
options.add_argument('user-data-dir=C:/Users/{username}/path to data of browser/')
- 另一种唯一的登录方式是使用隐身方式将用户代理伪造到提到的 DN here 并且效果很好。
- 我发现的主要缺点是,当自动化为 运行 时,您无法打开 另一个选项卡 ,否则,进程会中断。但这完美地解决了这个缺点。
- 但我发现的缺点是,一旦你登录,你就无法完成你的工作,因为你正在访问的网站会限制你并强制你更新浏览器才能访问该网站(Google Meet 在我的例子中)。 另一方面,从理论上讲,可以用用户数据打开自动化,但在新的 window 中。与 OAuth 以外的其他方法相比,我觉得它是最佳选择,因为它是实现它的最佳方式。
任何其他绕过这些限制的最佳工作建议Google?
最后,我成功绕过了 Selenium 中的 Google 安全限制,希望对您也有帮助。在这里分享整个代码。
简而言之:
- 您需要使用 old/outdated 用户代理并还原。
详细:
- 使用 selenium-stealth 伪造用户代理。
- 最初在登录前将用户代理设置为
DN
。 - 然后,登录后,恢复正常。(不是真的,但是 chrome v>80)
就是这样。
不需要保留用户数据,启用不太安全的应用程序访问,没有!
这是我目前可以使用的代码片段,虽然它很长!(包含评论以便更好地理解)。
# Import required packages, modules etc.. Selenium is a must!
def login(username, password): # Logs in the user
driver.get("https://whosebug.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-check\nand Re-Run the program.')
del username, password
driver.quit()
try:
WebDriverWait(driver, 60).until(lambda webpage: "https://whosebug.com/" in webpage.current_url)
print('\nLogin Successful!\n')
except TimeoutException:
print('\nUsername/Password seems to be incorrect, please re-check\nand 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 Whosebug after logging in
driver.switch_to.window(driver.window_handles[0]) # This switches to Whosebug website
driver.close() # This closes the Whosebug website
driver.switch_to.window(driver.window_handles[0]) # Focuses on present website
点击here了解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,
)
这对我有用。