3

I've been keep on getting NoSuchElementException whenever I try to get an web element. I know that the web element becomes present after some time. So, I've explicitly waited for 5-10 seconds until the web element gets loaded but even then I am still getting NoSuchElementException. Does anyone know how to prevent this exception? Here is my code:

String s=System.getProperty("user.dir");
System.setProperty("webdriver.chrome.driver", s+"\\Chrome090615\\chromedriver.exe");
ChromeDriver driver=new ChromeDriver();
try{
 driver.manage().window().maximize();
 driver.get("http://site21.way2sms.com/content/index.html");
 driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
 driver.findElementByXPath("//*[@id='username']").sendKeys("9XXXXXXXXX");
 driver.findElementByXPath("//*[@id='password']").sendKeys("XXXX");
 driver.findElementByXPath("//*[@id='loginBTN']").click();
 if(driver.findElementByXPath("//*[@id='ebFrm']/div[2]/div[1]/input").isDisplayed()==true)
 {
 driver.findElementByXPath("//*[@id='ebFrm']/div[2]/div[1]/input").click();
 }
 else {
 new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.id("ebFrm")));
 System.out.println("Waiting for 5 secs");
 }
 boolean ispresent=driver.findElementByXPath("//*[@id='sendSMS']/a").isDisplayed();
 driver.findElementByXPath("//*[@id='sendSMS']/a").click();
 System.out.println("Clicked sendSms");
 driver.findElementByXPath("//*[@id='sendSMS']/a").click();

I'm getting exception for the 'sendSMS' id. Whenever it tries to find this element, it throws NoSuchElementException all the time!

asked Jun 15, 2015 at 5:13
0

1 Answer 1

2

First of all, wait for the element you'll be clicking. Right now you're waiting for a different element.

Here's how: https://stackoverflow.com/questions/20903231/selenium-wait-until-element-is-present

Then, if you still get that error, the ID is probably wrong and thus not actually present on the page (in the same frame).

And why do you use XPath to locate by ID? Using findElementById is much cleaner.

answered Jun 15, 2015 at 5:26
6
  • Thanks @FDM for reply. trying your suggestion now and will update here thereafter!! Commented Jun 15, 2015 at 6:27
  • It didn't work out :-( though it gets explicitly wait for the specified time. I've tried all locators(i.e, id, csspath, xpath) but it fails to find that element. I don't know why this is happening all the time :-( Commented Jun 15, 2015 at 7:05
  • Do you have an URL on which we can test? Commented Jun 15, 2015 at 7:39
  • Can you please share the html snippet Commented Jun 15, 2015 at 8:25
  • 2
    Never mind guys..I found that i need to switch its iframe whereas this element is located. but anyway thanks for your replies ! Commented Jun 15, 2015 at 8:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.