想用selenum实现自动化登录,google加了检验,报此浏览器或应用可能不安全, 所以就想在在打开的浏览器直接操作元素是否可行而不是直接打开新浏览器, 直接上代码:
import os from selenium import webdriver from selenium.webdriver.common.by import By import time from selenium.webdriver.chrome.options import Options chrome_path = "C:\\Users\\win10\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe" profile_dir = "D:\\python-click\\selenium\\AutomationProfile" start_chrome_cmd = 'start %s --remote-debugging-port=9222 --user-data-dir=%s'%(chrome_path, profile_dir) os.system(start_chrome_cmd) #如果chrome_path 带有空格, 则start_chrome_cmd='start "" %s --remote-debugging-port=9222 --user-data-dir=%s'%(chrome_path, profile_dir) chrome_options = Options() chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222") driver = webdriver.Chrome(options=chrome_options) print("浏览器标题:",driver.title); def openUrl(url): driver.get(url) time.sleep(5) element = driver.find_element(By.LINK_TEXT, "登录") element.click() if __name__ == '__main__': openUrl('https://www.google.com/adsense/start/') 这个方式是可行的,至少手动点击登录,输入帐号都没有问题, 不过google仍然加了人机检验,如果直接自动点击输入,仍然会报"此浏览器或应用可能不安全"的问题而出不来密码输入框。我猜测是否加上模拟鼠标轨迹及点击位置随机生成可能会通过他的检验,现在还没有时间测试,有兴趣的可以自己测试一下。