如何避免每次重新登录我的账户,Selenium Python mac。

问题描述 投票:1回答:2

我正在学习Selenium(使用我的本地机器),需要用它来访问我的账户(FB,gmail等),想使用它而不必每次都输入我的用户名和密码。

有没有一种方法可以做到以下两种情况。

1) 让我的脚本中的任何指令都适用于我现有的chrome会话。

2) 以某种方式让加载的新窗口记住我的用户名密码(这似乎是使用现有的chrome会话,但我不确定如何)。

到目前为止,我有什么。

(我不确定我的个人资料是否应该包含我保存的谷歌和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)

这将打开一个新的窗口,里面有我的一些凭证,但之后就会崩溃,出现错误。

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)
python macos selenium selenium-webdriver webdriver
2个回答
1
投票

你可以这样做

browser = webdriver.Firefox()
browser.get("https:one_url.com")

time.sleep(1)

# Login to this one_url.com
username = browser.find_element_by_xpath("find username field")
password = browser.find_element_by_class_name("find password field")
username.send_keys("user")
password.send_keys("password")
login_attempt = browser.find_element_by_xpath("find submit button")
login_attempt.click()
time.sleep(5)

# enter into another web without login
browser.get("https://one_url/profile.com")
time.sleep(5)
# one more 
browser.get("https://one_url/profile.com")
time.sleep(5)

1
投票

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

根据我的工作,当在标准chrome中导航到chrome:/version时,会显示/Local/Google/Chrome/User Data/Profile 3。在这个配置文件中,我导航到StackOverflow并保存了证书。所以使用了以下代码

 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:/version交叉检查,在chrome驱动打开的配置文件路径显示为:\Local/Google/Chrome/User Data/Profile 3/Default。然后在该配置文件中自己手动登录,该配置文件由webdriver打开并通过关闭它来执行增益。

最后,页面显示为已登录。所以可能是java的问题。希望能帮你在python中试试。

谢谢你的帮助

© www.soinside.com 2019 - 2024. All rights reserved.