我想要实现的目标
我正在尝试登录到必须使用Selenium无头启用cookie的网站,我使用PhantomJS作为驱动程序.
问题
我首先使用Selenium IDE记录了这个程序,它使用Firefox(不是无头)工作正常.然后我将代码导出到Python,现在我无法登录,因为它抛出错误"只能为当前域设置Cookie".我不知道为什么我会遇到这个问题,我不在正确的域名上吗?
码
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
import unittest, time, re
self.driver = webdriver.PhantomJS()
self.driver.implicitly_wait(30)
self.base_url = "https://login.example.com"
driver = self.driver
driver.get(self.base_url)
all_cookies = self.driver.get_cookies()
# It prints out all cookies and values just fine
for cookie in all_cookies
print cookie['name'] + " --> " + cookies['value']
# Set cookies to driver
for s_cookie in all_cookies:
c = { s_cookie['name'] : s_cookie['value']}
# This is …Run Code Online (Sandbox Code Playgroud) 我想要:
我想要做的是登录ONCE,从中保存cookie,并将它们添加到我想要打开的任何其他FirefoxDriver.
现在我正在尝试保存cookie
public ReadOnlyCollection<Cookie> Cookies { get; set; }
Run Code Online (Sandbox Code Playgroud)
这是结果
WebDriver.Manage().Cookies.AllCookies;
Run Code Online (Sandbox Code Playgroud)
假设登录工作和cookie保存在上面,我有这个:
WebDriver = new FirefoxDriver();
WebDriver.Navigate().GoToUrl("http://www.example.com");
if (cookies != null)
{
var s = WebDriver.Manage().Cookies; //Logged out cookies
WebDriver.Manage().Cookies.DeleteAllCookies(); //Delete all of them
var sd = WebDriver.Manage().Cookies; //Make sure theyre deleted
foreach (var cookie in cookies)
{
WebDriver.Manage().Cookies.AddCookie(cookie);
}
var ss = WebDriver.Manage().Cookies;
WebDriver.Navigate().GoToUrl("http://example.com/requiresloginpage");
}
Run Code Online (Sandbox Code Playgroud)
问题是,在这种情况下,超过"ss"会产生此异常错误
AllCookies = 'ss.AllCookies' threw an exception of type
'OpenQA.Selenium.WebDriverException'
base {System.Exception} = {"Unexpected problem getting …Run Code Online (Sandbox Code Playgroud) 无论如何,那司机能记住logged in session,所以它不会带我回到Login页面(例如喜欢google-chrome)吗?
这就是我现在正在做的事情
public static void main(String[] args) throws Exception {
driver = new ChromeDriver();
driver.get("http://localhost/interestingStuff); //I get redirected to login page
login(); //logins in to the page and submits(note I try to omit this part if possible).
doStuff();
driver.close(); //I want to be able to keep session next time I start this program.
}
Run Code Online (Sandbox Code Playgroud)