-1

I want to know how could we input a variable and some keys in only one sendkeys method. In fact in a textfield, I want to input "Ctrl+A" to select the current value, and replace it with a new value which is stocked in a varialbe called "orderNumber".

For example :

driver.findElement(By.xpath("//tr[@class='nodrag nodrop filter row_hover']/th[@class='text-center'
[2]/input[@class='filter']")).sendKeys(Keys.CONTROL +"a");

Then

driver.findElement(By.xpath("//tr[@class='nodrag nodrop filter row_hover']/th[@class='text-center'
[2]/input[@class='filter']")).sendKeys(orderNumber);

Is there a way to combine these 2 lines into 1 line? I tried this it doesn't work.

driver.findElement(By.xpath("//tr[@class='nodrag nodrop filter row_hover']/th[@class='text-center'
[2]/input[@class='filter']")).sendKeys(Keys.CONTROL +"a" , orderNumber);

Thank you in advance for your answers.

JeffC
  • 22,180
  • 5
  • 32
  • 55
  • http://artoftesting.com/automationTesting/handling-keyboard-events-in-selenium.html – Gautam Bothra May 24 '18 at 11:19
  • Possible duplicate of [Selenium WebDriver: I want to overwrite value in field instead of appending to it with sendKeys using Java](https://stackoverflow.com/questions/3249583/selenium-webdriver-i-want-to-overwrite-value-in-field-instead-of-appending-to-i) – JeffC May 24 '18 at 19:05

2 Answers2

1

If you want to do that, you could use the clear() function in the textfield web element.

So, for example:

        ChromeDriver driver = new ChromeDriver();

        driver.get("https://www.google.co.uk/");

        WebElement el =driver.findElement(By.className("gsfi"));
        el.sendKeys("hello!");

        el.clear();

        int orderNumber=1;

        el.sendKeys(String.valueOf(orderNumber));
Davide Patti
  • 3,391
  • 2
  • 18
  • 20
0

You can do it using following code,

WebElement ele = driver.findElement(By.xpath("//tr[@class='nodrag nodrop filter row_hover']/th[@class='text-center'
[2]/input[@class='filter']"));
ele.clear();
ele.sendkeys(orderNumber);
Murthi
  • 5,299
  • 1
  • 10
  • 15