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!

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.

| improve this answer | |
  • Thanks @FDM for reply. trying your suggestion now and will update here thereafter!! – lss mesy Jun 15 '15 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 :-( – lss mesy Jun 15 '15 at 7:05
  • Do you have an URL on which we can test? – FDM Jun 15 '15 at 7:39
  • Can you please share the html snippet – QAMember Jun 15 '15 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 ! – lss mesy Jun 15 '15 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.