简体   繁体   English

如何在Selenium Webdriver JavaScript脚本中设置Cookie?

[英]How do I set a cookie in the selenium webdriver javascript script?

I want to set a cookie on a simple webdriver script: 我想在一个简单的webdriver脚本上设置一个cookie:

WDS.sampleResult.sampleStart();


//WDS.driver.manage().addCookie(new Cookie("connect.sid", "s%3AeexeZcd_-S23Uh30e3Dmd4X9PskWF08s6m5hDurDa5Jj66SupmmiqvKEjAg6HGigl0o0V%2B9R7m4", "api." + rootdomain, "/", null));

// start test
WDS.browser.get("https://api." + rootdomain);
waitForVisible(pkg.By.linkText("Log In"));
WDS.browser.findElement(pkg.By.linkText("Log In")).click()

How do I do that? 我怎么做?

Something like: 就像是:

var cookie = new org.openqa.selenium.Cookie("name","value");
WDS.browser.manage().addCookie(cookie);

should do the trick. 应该可以。

See: 看到:

You can only set the cookie when the driver has started. 您只能在驱动程序启动后设置cookie。 The driver starts when you do a .get(). 执行.get()时,驱动程序启动。 Add the cookie after the .get() and it will work. 在.get()之后添加cookie,它将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过 Selenium 和 WebDriver 等待 JavaScript __doPostBack 调用 - How do I wait for a JavaScript __doPostBack call through Selenium and WebDriver Selenium-webdriver / Javascript:如何滚动页面? - Selenium-webdriver/Javascript: How do I scroll through a page? 如何使用selenium-webdriver正确添加cookie? - How can I correctly add a cookie using selenium-webdriver? 在 JavaScript 中保存 cookie 值时如何设置路径? - How do I set path while saving a cookie value in JavaScript? 如何在JavaScript中使用XMLHttpRequest设置Cookie(标题)? - How do I SET a Cookie (header) with XMLHttpRequest in JavaScript? 如何使用javascript设置cookie的HttpOnly标志? - How do I set the HttpOnly flag of a cookie with javascript? 如何在 Javascript 中设置 cookie 在 1 小时内过期? 有了这个脚本 - How to set a cookie to expire in 1 hour in Javascript? With this script 如何通过 selenium-webdriver javascript API 设置“debuggerAddress”chromeOption? - How to set "debuggerAddress" chromeOption via selenium-webdriver javascript API? 如何使用Selenium WebDriver和NodeJS测试JavaScript错误? - How do you test JavaScript errors with Selenium WebDriver and NodeJS? 如何等待直到设置了cookie? - How do I wait until a cookie is set?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM