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!
1 Answer 1
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.
-
Thanks @FDM for reply. trying your suggestion now and will update here thereafter!!lss mesy– lss mesy2015年06月15日 06:27:16 +00:00Commented 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 :-(lss mesy– lss mesy2015年06月15日 07:05:36 +00:00Commented Jun 15, 2015 at 7:05
-
Do you have an URL on which we can test?FDM– FDM2015年06月15日 07:39:20 +00:00Commented Jun 15, 2015 at 7:39
-
Can you please share the html snippetQAMember– QAMember2015年06月15日 08:25:59 +00:00Commented Jun 15, 2015 at 8:25
-
2Never mind guys..I found that i need to switch its iframe whereas this element is located. but anyway thanks for your replies !lss mesy– lss mesy2015年06月15日 08:49:07 +00:00Commented Jun 15, 2015 at 8:49