8

I am trying to automate a website using selenium in IE 10. The site opens fine however when I want to click on a element(button) it finds the element and clicks on it as well however the elements state(button name changing) which needs to be changed doesn't change.

Here is my code.

   File file = new File("D:/IEDriverServer.exe");
   System.setProperty("webdriver.ie.driver", file.getAbsolutePath() );

   DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
   capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
   true); 

   WebDriver driver = new InternetExplorerDriver(capabilities);
   driver.get("http://www.midomi.com");
   driver.findElement(By.id("searchMovielanding")).click();

I tried on two machines. On one machine the code ran properly and on another the dont see the click event changing the element state. I checked for the element on the webpage and found it however dont know why it is not clicking it properly on one machine.

  if(driver.findElements(By.id("searchMovielanding")).size() != 0) {
 System.out.println("Element Found");
 }

Any help to resolve this appreciated.

Wanderer
  • 366
  • 2
  • 6
  • 18
  • Would need to see the site to be able to help you. Otherwise, this is just a guessing game. See: https://stackoverflow.com/help/mcve – SiKing Jul 18 '14 at 16:30

8 Answers8

10

Try the below.

driver.findElement(By.id("searchMovielanding")).sendKeys(KEYS.ENTER);

In IE, some times click does not work.

Purus
  • 5,701
  • 9
  • 50
  • 89
  • 2
    Can you explain *In IE, some times click does not work*? – Ajinkya Jul 18 '14 at 05:47
  • 1
    I now had the same problem in IE. It worked for weeks but now element.click() is only selecting it and not issue a real click. – Martin Kersten Jan 09 '15 at 17:39
  • thats true, even I am struggling with the same issue, i had to use `keys.enter` too... – Mohammed Sufian Feb 02 '16 at 08:13
  • 1
    Update: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/Keys.html **`Keys.ENTER`** – Vy Do Sep 26 '16 at 02:45
  • 1
    IE requires the mouse in order to get the click to work, so for headless mode or if you're not actually on the server and the mouse isn't available to the driver, the click action will fail. Another reason is if you're selecting an element that is "not displayed", you wouldn't be able to use the .click() method nor this .sendKeys(Keys.Enter) method. Other option is to do: `IJavaScriptExecutor.ExecuteScript("arguments[0].click();", element);` Found this to be the most reliable way of getting clicks to work in selenium across all browsers/devices. – mche Mar 28 '19 at 00:15
2

This is actually a defect in the Selenium InternetExplorerDriver and they are not currently planning to address it. The link also has some suggested workarounds. However they did not work too well for me. I'll update if I can find anything. https://code.google.com/p/selenium/issues/detail?id=4403

KyleK
  • 626
  • 6
  • 7
2

The below trick worked for me. Add the code snippet where browser selection

// Setting attribute native Events to false enable click button in IE
DesiredCapabilities caps = DesiredCapabilities.internetExplorer(); caps.setCapability("ignoreZoomSetting", true);
caps.setCapability("nativeEvents",false);
WebDriver driver = new  InternetExplorerDriver(caps);
Ilya Chumakov
  • 23,161
  • 9
  • 86
  • 114
Jag
  • 21
  • 1
1

Adding up on previous answer. Use the following solution to click on links:

driver.findElement(By.id("searchMovielanding")).sendKeys(KEYS.ENTER);

However, ENTER key will trigger a submit on HTML forms BEFORE the click on the button. SPACE key on the other hand, clicks a button and does not submit the form (unless that is what the button is supposed to do). So suggest you rather use the following for buttons:

driver.findElement(By.id("searchMovielanding")).sendKeys(KEYS.SPACE);
1

I had the same issue with IE 11. Hope this will work for you as well.

capabilities.setCapability("nativeEvents",false);
capabilities.setCapability("ignoreZoomSetting", true);

If it's not work, try to perform click using javascript as a workaround.

1

I have faced the same issue in the past and would recommend using Actions class for clicking in IE10 or IE11

Actions act = new Actions(driver);
WebElement p=driver.findElement(By.id("element id"));
act.click(p).build().perform();

You may also use Javascript Executor for the same

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("return document.getElementById('ELEMENT ID').click();");
0

I would recommend not to use

capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);

as it bypasses the Protected Mode settings in IE browser. Read more here: http://jimevansmusic.blogspot.in/2012/08/youre-doing-it-wrong-protected-mode-and.html

In case you are getting any error while running without setting above capability in the code please make sure you have followed all the required configurations: https://code.google.com/p/selenium/wiki/InternetExplorerDriver#Required_Configuration

Now, rerun your test and fork me what you experience.

Sitam Jana
  • 3,123
  • 2
  • 21
  • 40
0

Using IJavaScriptExecutor solves the problem of clicking in IE many times. As it happened in my case when I was trying to click an element in the left frame of my application it was clicking undesired link but I was able to click on desired element by using below code

IWebElement element_xpathAddVar = Browser.Driver.FindElement(By.XPath("//*[@id='td_48']/a"));
js.ExecuteScript("arguments[0].click();", element_xpathAddVar);
Benjamin Lowry
  • 3,730
  • 1
  • 23
  • 27