如何避免每次Selenium Python mac都重新登录我的帐户 [英] How to avoid re-logging into my accounts each time, Selenium Python mac

查看:258
本文介绍了如何避免每次Selenium Python mac都重新登录我的帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Selenium(使用本地计算机),需要使用它来访问我的帐户(FB,Gmail等),并且希望在无需每次输入用户名和密码的情况下使用它.

I am learning Selenium (using my local machine) and need to use it to access my accounts (FB, gmail, etc) and would like to use it without having to input my user id and password each time.

有没有办法做到这一点?

Is there a way to either:

1)脚本中的指令是否适用于我现有的chrome会话

1) Have whatever instructions are in my script apply to my existing chrome session

2)不知何故,载入新窗口时会记住我的用户名/密码(这似乎在使用现有的chrome会话,但我不确定如何使用)

2) Somehow have the new window that loads remember my user name/password (this seems like using an existing chrome session, but im not sure how)

我到目前为止所拥有的:

(我不确定我的个人资料是否应包含我保存的Google和FB等网站的凭据,但我不知道为什么不这样做)

chrome_options = Options()
chrome_options.add_argument("--user-data-dir=/Users/username/Library/Application Support/Google/Chrome/Default")
driver = webdriver.Chrome(chrome_options=chrome_options)

这会打开一个包含我的一些凭据的新窗口,但此后崩溃并显示错误.

This opens up a new window with some of my credentials, but crashes thereafter with error.

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
  (Driver info: chromedriver=2.14.313457 (3d645c400edf2e2c500566c9aa096063e707c9cf),platform=Mac OS X 10.9.5 x86_64)

推荐答案

在Java中,我们可以使用ChromeOptions和Chrome Profile来实现.在chrome中,导航到chrome://version/,它将显示配置文件路径和可执行路径.

In Java, we can do it by using ChromeOptions and Chrome Profile. In chrome navigate to chrome://version/ It will display profile path and Executable path.

根据我的工作,导航至标准chrome中的chrome://version/时,会显示\ Local \ Google \ Chrome \ User Data \ Profile 3.在此配置文件中,我导航到 StackOverflow 并保存凭据.所以在下面的代码中使用

As per my working on this, \Local\Google\Chrome\User Data\Profile 3 is displaying when navigating to chrome://version/ in standard chrome. In this profile, I navigated to StackOverflow and saved credentials. So used below code

 Map<String, Object> prefs = new HashMap<String, Object>();
    prefs.put("binary", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");

    System.setProperty("webdriver.chrome.driver", "E:\\selenium_setups\\poi-3.12\\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();

    options.setExperimentalOption("prefs", prefs);
    options.addArguments("user-data-dir=C:\\Users\\murali\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 3");
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    WebDriver driver = new ChromeDriver(capabilities);

    //WebDriver driver = new ChromeDriver(options);
    driver.get("http://stackoverflow.com/");

据我了解,我不显示显示为已登录的stackoverflow.com页面.但是第一次,我没有登录.因此在驱动程序,配置文件打开的chrome中对chrome://version/进行了交叉检查.路径显示为
\ Local \ Google \ Chrome \ User Data \ Profile 3 \ Default.然后手动登录该配置文件本身,该配置文件由webdriver打开,并通过关闭该文件执行增益.

As per my understanding, I excepted the stackoverflow.com page displayed as logged in. But for the first time, I am not logged in. so cross-checked with chrome://version/ in chrome opened by driver, profile path is displayed as
\Local\Google\Chrome\User Data\Profile 3\Default. Then logged manually in that profile itself, which is opened by webdriver and executed gain by closing it.

最后,页面显示为已登录.因此它可能是在Java中.我希望它能帮助您尝试使用python.

Finally, the page is displayed as logged in. So it may be in java. I hope it will help you to try in python.

谢谢

这篇关于如何避免每次Selenium Python mac都重新登录我的帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆