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

标签 java selenium webdriver

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

Steps:
1. Open login page 
2. Log in
3. 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/

相关文章:

python - 使用变量而不是文字时出现断言错误

javascript - BrowserStack 自动化测试 - 单击操作在 Android 上不起作用

java - 使用 XOR Shift 作为更快的 CRC32 校验和?

c# - 从 Excel Feed 获取实时值

java - 从 selenium webdriver 中的消息中获取特定值

python - 如何在 Selenium Webdriver Python 3 中使用 Chrome 配置文件

java - 当在 webdriver 的页面中找不到 webElement 时,不会出现 NoSuchElementexception

java - 我想将一个数字拆分为一位数字并将其更改为android中的二进制

java - 尝试安装deeplearning4j

python - 使用xpath中的Starts with和ends函数查找selenium元素