google-chromecookieskotlinhtmlunit-driver

HtmlUnitDriver open url witch chrome cookie


(translated) I use this code:

println("load cookies...")
val cookies: Set<DecryptedCookie> = ChromeBrowser().cookies as Set<DecryptedCookie>
println("load client...")
val browser = HtmlUnitDriver(true)

println("transfer cookies...")
for(cookie in cookies)
{
    println(cookie.domain)
    browser.manage().cookies.add(Cookie(cookie.name, cookie.decryptedValue, cookie.domain, cookie.path, cookie.expires))
}

println("load url...")
browser.manage().timeouts().pageLoadTimeout(2, TimeUnit.SECONDS)
browser.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS)
browser.manage().timeouts().pageLoadTimeout(2, TimeUnit.SECONDS)
browser.get("http://www.cyberforum.ru")
println(browser.pageSource)

But open link no loggedin. Import cookie with chrome sort of like successful...


Solution

  • I decided. It was necessary to add through the method in Manage. Also can only used url and refresh^

    val domain = "www.cyberforum.ru"
    val site = "http://www.cyberforum.ru/"
    
    val cookies = ChromeBrowser().getCookiesForDomain(domain) as Set<DecryptedCookie>
    val browser = HtmlUnitDriver(true)
    
    browser.manage().timeouts().pageLoadTimeout(2, TimeUnit.SECONDS)
    browser.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS)
    browser.manage().timeouts().pageLoadTimeout(2, TimeUnit.SECONDS)
    
    browser.get(site)
    
    for(cookie in cookies)
        browser.manage().addCookie(Cookie(cookie.name, cookie.decryptedValue, cookie.path, cookie.expires))
    
    browser.get(site)
    println(browser.pageSource)