Get user input from IDE/Console in selenium webder

2020-07-27 06:24发布

问题:

I want to get the key(send key) from user input how should I implement it?

driver.findElement(By.xpath(".//*[@id='vehicleNum']")).sendKeys("1121");

in input box what ever user type i want gen then i want to send through selenium?

回答1:

Even your question is not clear properly but as I am assuming you need below solution. To get the input value from textbox, you can use below code:

 driver.findElement(By.xpath(".//*[@id='vehicleNum']")).sendKeys("1121");
 String str = driver.findElement(By.xpath(".//*[@id='vehicleNum']")).getAttribute("value");
 System.out.println(str);

Program output will be "1121"



回答2:

You can use builtin Scanner class to get the input from system console. Below code might give you some idea.

Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
//now pass name in sendkeys.
driver.findElement(By.xpath(".//*[@id='vehicleNum']")).sendKeys(name);

Else if you need to get the text from one of the webelement, identify the webelement and use getText() to get the string value.

String value = driver.findElement(by.xpath("//")).getText();
driver.findElement(By.xpath(".//*[@id='vehicleNum']")).sendKeys(value);

Hope this helps. Thanks..



回答3:

As a demo I am providing you a sample block of code with respect to the URL https://accounts.google.com which will ask the Input from User to provide Email or Phone and then click on Next button:

Using Scanner you will get the user prompt on the IDE console as Enter your Email or Phone :. Do remember to close the scanner explicitly through scanner_user.close(); to prevent Resource Leakage.

import java.util.Scanner;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class GMAIL_LOGIN_SCANNER 
{

    public static void main(String[] args) 
    {


        Scanner scanner_user, scanner_pass;
        System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("start-maximized");
        options.addArguments("disable-infobars");
        options.addArguments("--disable-extensions"); 
        WebDriver driver =  new ChromeDriver(options);
        driver.get("https://accounts.google.com");
        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
        scanner_user = new Scanner(System.in);
        System.out.println("Enter your Email or Phone : ");
        String user = scanner_user.nextLine();
        driver.findElement(By.xpath("//input[@id='identifierId']")).sendKeys(user);
        driver.findElement(By.id("identifierNext")).click();
        scanner_user.close();
    }

}