gpt4 book ai didi

java - Selenium webdriver - 保持用户登录(java)

转载 作者:行者123 更新时间:2023-12-01 13:13:02 24 4
gpt4 key购买 nike

我正在尝试使用 webdriver (firefox) 进行简单的 Selenium 测试。

Steps:1. Open login page 2. Log in3. Open subpage

My problem is that when a subpage is loaded user is no longer logged in. Thanks in advance for tips how to solve this.

public class Test  {
public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();

driver.get("https://example.com/login");

WebElement login = driver.findElement(By.id("login_username"));
WebElement pass = driver.findElement(By.id("login_password"));
WebElement loginButton = driver.findElement(By.id("auth-login-submit"));

login.sendKeys("login");
pass.sendKeys("pass");
loginButton.click();

driver.get("https://example.com/subpage");
}
}

最佳答案

Thanks a lot, it helped. I use Thread.sleep(5000) in try/catch. Is there a better way? – aponski Mar 28 at 9:00

隐式等待是最佳选择

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.

这只是一次性配置

来源:http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp

关于java - Selenium webdriver - 保持用户登录(java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22691402/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com