Y
Y
Yasuro2018-10-11 10:27:40
Java
Yasuro, 2018-10-11 10:27:40

How to use cookies in selenium?

You need to go to the site with native cookies (already authorized or, for example, already with a filled basket in the online store)
What I tried:
1)

System.setProperty("webdriver.chrome.driver","E:\\Roma\\prog\\Java\\project\\yandex\\driver\\chromedriver.exe");
        ChromeOptions opt = new ChromeOptions();
        opt.setBinary("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
        opt.addArguments("--user-data-dir=C:\\Users\\Roma\\AppData\\Local\\Google\\Chrome\\User Data");

        ChromeDriver driver=new ChromeDriver(opt);

        driver.get("https://site.ru/checkout");

This is the code to open your native chrome, not the one that selenium opens by default.
gives error:
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
2)
Cookie cookie = new Cookie.Builder("name", "value")
    .domain(".mydomain.com")
    .expiresOn(new Date(2015, 10, 28))
    .isHttpOnly(true)
    .isSecure(false)
    .path("/mypath")
    .build();

driver.manage().addCookie(cookie);

it’s just that nothing happens, it’s possible because I didn’t set the correct cookies, because it’s impossible to parse them normally.
3) there was an idea to open the selenium browser, log in there, and then open the same page in the second tab and work with it. But there was not enough sense to implement it.
I like the first option more, when I just open my native browser in which all the cookies are already there, but an error falls out.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan, 2018-10-13
@Iv_and_S

"open selenium browser, log in there"
for me, this solution is much better than the two options above.
so what exactly is the problem?
Did you fill out the form and log in?
The only thing, it is important to understand that in selenium you need to switch browser tabs, do not forget if you switch to another one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question