2

I have the below code which will click on a button in window. On clicking the button,the current window is closed and new window will be opened. Some text will be inputted in a textbox in new window.

WebElement element=null;
        try {
            driver.getWindowHandles();
            for (String winHandle : driver.getWindowHandles()) {
                    driver.switchTo().window(winHandle);
                    try {
                            element = driver.findElement(By.xpath("//*[@id='username']"));
                    } catch (Exception e) {
                        continue;
                    }
                    if (element.isDisplayed()) {
                        windowFound = 1;
                        break;
                    }
                }
            }
element.sendKeys("Testingusername");

Last line to input send keys is not failing. But the actual text is not entered into the textbox.

This works well in chrome. Issue is with Internet explorer only. Selenium : 2.53.1 IE 11

manutd
  • 291
  • 1
  • 5
  • 18
  • You should first click on the text box for focus using `element.click()` then use `element.sendKeys("Testingusername");`.. – Saurabh Gaur Jul 27 '16 at 06:38
  • I tried that also but it dint work out. I tried inputting using javascript. It worked but I should not go with that approach since after that there are many input I need to do in that window. – manutd Jul 27 '16 at 07:01
  • try once using `Actions` as `Actions act = new Actions(driver); act.sendKeys(element, "Testingusername")` and let me know.. – Saurabh Gaur Jul 27 '16 at 07:07
  • Please, please don't use XPath to find an element by just an ID. `By.id()` was created specifically for this purpose and is much faster. – JeffC Jul 27 '16 at 17:12

5 Answers5

1

Try to focus on the element let say

element.Clear();
element.sendKeys("testingUserName");

and put this code to try catch to see if you get any exceptions

Leon Barkan
  • 2,676
  • 2
  • 19
  • 43
1

Few things :

  • verify if you've located the correct element in IE as it sometimes XPath behavior is different in IE.

  • try to confirm the attributes of the element under question with the attributes observed in other browsers.

  • try using IE Driver 32 bit version for IE11 browser.

  • if nothing works then there is no harm in using javascript sendKeys. it's not a bad practise

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
Mrunal Gosar
  • 4,595
  • 13
  • 48
  • 71
  • Thanks for your reply!For the first and second point, in IE itself I am inputting many times during the execution. But on this particular scenario of switching and input is not working. So I guess there is no issue with locators. For the last point, there are many inputs which I need to do after this. Which I have already scripted. No sendKeys is working in that window. So I'm not preferring to use a javascript. I will try the third option but still wondered why to use a 32bit version. – manutd Jul 27 '16 at 10:13
  • Ok..can you do one more thing and add one desiredcapabilities to InternetExplorerDriver.ENABLE_PERSISTENT_HOVERING and InternetExplorerDriver.REQUIRE_WINDOW_FOCUS and try..Please note while running this do not touch your mouse..then just before entering any text click on the control and see if its the correct text box where mouse pointer clicks on and brings the focus on that text box – Mrunal Gosar Jul 27 '16 at 12:51
1
Actions a = new Actions(driver);
a.SendKeys(element, "Your text to input").Build().Perform();

Note: Works in IE11

thokuest
  • 5,800
  • 24
  • 36
0

try this one This works for me

WebElement element=null;
        try {
            driver.getWindowHandles();
            for (String winHandle : driver.getWindowHandles()) {
                    driver.switchTo().window(winHandle);
                    try {
                            element = driver.findElement(By.xpath("//*[@id='username']"));
                    } catch (Exception e) {
                        continue;
                    }
                    if (element.isDisplayed()) {
                        windowFound = 1;
                        break;
                    }
                }
            }
element.click();

String text = "your text that you want to enter";
StringSelection stringSelection = new StringSelection(text);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
selva
  • 1,175
  • 1
  • 19
  • 39
-1

I think it's all about timing. You should consider adding Thread.Sleep(3000); to your code:

Thread.Sleep(3000);
element.sendKeys("Testingusername");

Tài Hoàng
  • 61
  • 1
  • 10