5

I am trying to click the image icon via xPath but when i run the code the link present on image icon is not opening. could you please help me in resolving this issue.

The Code i used to click the mail icon : To Click the Mail Ico

   driver.findElement(By.xpath("//*[@id='e-switcher-mail-icon']")).click();

Html of the page Html of page-part2

user1564024
  • 81
  • 1
  • 1
  • 9
  • 2
    Not without any knowledge about the page. Could you post the relevant part of the page's HTML? Are there any iframes? Is it possible that the application is AJAX-loaded and you might therefore need to wait a bit for the element to get loaded (see [implicit and explicit waits](http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#explicit-and-implicit-waits))? Is there any exception thrown? Or does it simply freeze? Have you tried other browsers? – Petr Janeček Sep 12 '13 at 01:54
  • I am getting the below error once the script is executed..org.openqa.selenium.NoSuchElementException: no such element (Session info: chrome=29.0.1547.66) (Driver info: chromedriver=2.2,platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 30.04 seconds – user1564024 Sep 12 '13 at 18:18
  • I have executed this script in Chrome and in internet explorer i couldnt execute because everytime i click on the link it opens in a new window and the cookie is being lost. Hence i used Chrome alone. – user1564024 Sep 12 '13 at 18:47
  • In the HTML page i could see there are few frames used but they are not iframes. I have attached the html page code in my question. Please look into it. – user1564024 Sep 12 '13 at 19:23

3 Answers3

3

As slanec said more information is required or might be the element is not loaded. If u feel that the element has loaded and still its not happening, using java script is one way to click the image element.

Something like this

WebElement element = driver.findElement (By.xpath ("//*[@id='e-switcher-mail-icon']"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript ("arguments[0].click();" , element);
Sriram
  • 449
  • 3
  • 8
  • 15
  • I tried by using ur approach but it is throwing the below error:org.openqa.selenium.NoSuchElementException: no such element (Session info: chrome=29.0.1547.66) (Driver info: chromedriver=2.2,platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 30.04 seconds – user1564024 Sep 12 '13 at 18:19
  • Thats because u have frames. Please use something this way driver.switchTo().frame(driver.findElement(By.id("frame name")); Try to switch to both the frames and then perform the above action. U will have switch to both the frames. One for e-frame.. and one for s_mainframe... then try suing the above statement for clicking image or the normal driver.find.... .click(). Try and let me know – Sriram Sep 13 '13 at 08:38
  • driver.switchTo().frame(driver.findElement(By.id("e-frameset"))); driver.switchTo().frame(driver.findElement(By.id("s_MainFrame"))); i have used the above code lines then followed by code line driver.findElement(By.xpath("//*[@id='e-switcher-mail-icon']")).click(); but stil i am getting NOSuchElementException. – user1564024 Sep 13 '13 at 18:28
  • Correct. Now try using the java script code instead of driver.findElement(By.xpath("//*[@id='e-switcher-mail-icon']")).click(); And also add some wait statment once after the frame switch. Try and let me know – Sriram Sep 16 '13 at 06:19
  • I have used driver.switchTo().frame(driver.findElement(By.id("e-frameset"))); driver.switchTo().frame(driver.findElement(By.id("s_MainFrame"))); Wait wait= new FluentWait(driver).withTimeout(15L, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS); WebElement element = driver.findElement (By.xpath ("//*[@id='e-switcher-mail-icon']")); JavascriptExecutor executor = (JavascriptExecutor) driver; executor.executeScript ("arguments[0].click();" , element);driver.switchTo().defaultContent(); but still i am getting NOSuchElementException. – user1564024 Sep 18 '13 at 23:18
2

Could be that the element hasn't yet loaded in the DOM. Try waiting for expected conditions:

Wait<WebDriver> wait= new FluentWait<WebDriver>(driver).withTimeout(15L, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS);

WebElement icon = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='e-switcher-mail-icon']")));

icon.click();
Dingredient
  • 2,191
  • 22
  • 47
  • I have used your approach but still no luck it throws the same below error message: org.openqa.selenium.NoSuchElementException: no such element (Session info: chrome=29.0.1547.66) (Driver info: chromedriver=2.2,platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 30.04 seconds – user1564024 Sep 12 '13 at 18:20
0

Tada

Touché. It's the frames. Either <frame> or <iframe>, both need special care, see the documentation on the topic.

What you need to do:

driver.switchTo().frame("s_MainFrame");

after this, the driver's context will switch to the frame and all searches will be done in it, so you should be able to find the element without any further problems.

Once you're done in the frame and you need to switch back to the default context of the page, do:

driver.switchTo().defaultContent();
Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
  • I tried it but no luck, i am still getting the NoSuchElement Exception. Do u want me to send the entire code? – user1564024 Sep 12 '13 at 22:25
  • @user1564024 Try to experiment with it a little. Make sure you switched to the right frame. If yes, then I fear I'm done here as I don't know what else could have caused it. – Petr Janeček Sep 12 '13 at 22:39