【问题标题】:instead of getting user input in console i need to get text in field username in webpage而不是在控制台中获取用户输入,我需要在网页中的字段用户名中获取文本
【发布时间】:2017-08-21 05:52:50
【问题描述】:

我需要在网页的用户名字段中获取文本,而不是在控制台中获取用户输入,我的代码:

 public void loginpage(){


        driver.findElement(By.id("Username")).clear();
        System.out.println("Username: ");
        Scanner scan1 = new Scanner(System.in);
        String input1 = scan1.nextLine();
        System.out.println(input1);

        driver.findElement(By.id("password")).clear();
        System.out.println("password: ");
        Scanner scan2 = new Scanner(System.in);
        String input2 = scan2.nextLine();


        Select drp= new Select(driver.findElement(By.name("AdminTypeId")));
        drp.selectByVisibleText("Super Admin");

        driver.findElement(By.id("login-submit")).click();
    }

【问题讨论】:

  • 您能告诉我们您尝试自动化的确切手动步骤吗?

标签: java selenium selenium-webdriver automated-tests


【解决方案1】:

您正在寻找sendKeys()

WebElement userName = driver.findElement(By.id("Username"));
userName.clear();
userName.sendKeys(input1);

【讨论】:

    【解决方案2】:

    如果需要从网页中获取文本

    你必须使用get命令..说如果需要从页面获取文本然后

    driver.findElement(By.id("Username")).getText();
    

    如果需要获取任何属性说值然后

    driver.findElement(By.id("Username")).getAttribute("value")
    

    如果您需要在网页中输入文字

    然后使用发送键

    driver.findElement(By.id("Username")).sendKeys("input text here");
    

    在这里你可以在senkeys中传递input1

    driver.findElement(By.id("Username")).sendKeys(input1);
    

    【讨论】:

    • 谢谢你能给我发送代码,我们必须通过键盘进行登录输入,这些值存储在网站登录页面中
    • driver.findElement(By.name("LoginText1")).clear(); System.out.println("用户名:");扫描仪 scan1 = new Scanner(System.in);字符串输入1 = scan1.nextLine(); System.out.println(input1);而不是简单地将用户的输入返回到控制台,有没有办法让它将它插入到文本字段中。我想将它插入到名为 LoginText1 的字段中。这是用于使用 Selenium WebDriver 进行网站测试的。我是 Java 新手,非常感谢所有帮助。
    • 试试这个 driver.findElement(By.name("LoginText1")).clear(); System.out.println("用户名:");扫描仪 scan1 = new Scanner(System.in);字符串输入1 = scan1.nextLine(); driver.findElement(By.name("LoginText1")).sendKeys(input1);
    猜你喜欢
    • 2013-04-24
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    • 2014-12-28
    相关资源
    最近更新 更多