Exception in thread "main" org.openqa.selenium.NoSuchElementException: When trying to select an element from a pop-up using selenium

Kaustubh :

I'm trying to select create a contact in my website. But I'm not able to select a user from a pop-up. You can have a look here (this is my test site where I'm trying to perform the below code).

Here is my test script:

driver.get("http://automation.cloudaccess.host/administrator/index.php?option=com_contact&view=contacts");
driver.findElement(By.id("mod-login-username")).sendKeys("admin");
driver.findElement(By.id("mod-login-password")).sendKeys("admin@123");
driver.findElement(By.id("mod-login-password")).submit();
driver.findElement(By.xpath("//button[@onclick=\"Joomla.submitbutton('contact.add');\"]")).click();
driver.findElement(By.id("jform_name")).sendKeys("James");
driver.findElement(By.xpath("//div[@class='input-append']/a")).click();
driver.switchTo().frame("field-user-modal");
driver.findElement(By.id("filter_search")).sendKeys("admin");
driver.findElement(By.xpath("//button[@type='submit']")).click();
driver.findElement(By.linkText("admin")).click();        
driver.findElement(By.xpath("//button[@onclick=\"Joomla.submitbutton('contact.save');\"]")).click();

Here is error I'm getting:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"filter_search"}
  (Session info: chrome=66.0.3359.181)
  (Driver info: chromedriver=2.40.565383 (76257d1ab79276b2d53ee976b2c3e3b9f335cde7),platform=Linux 4.15.0-24-generic x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T15:15:08.936Z'
System info: host: 'vowellt4', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.15.0-24-generic', java.version: '1.8.0_171'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.40.565383 (76257d1ab79276..., userDataDir: /tmp/.org.chromium.Chromium...}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: LINUX, platformName: LINUX, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 66.0.3359.181, webStorageEnabled: true}
Session ID: b3b7fc635039d93fe22d661995bb69a3
*** Element info: {Using=id, value=filter_search}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
    at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:317)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:363)
    at org.openqa.selenium.By$ById.findElement(By.java:188)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:309)
    at testScripts.Test.main(Test.java:36)

I have checked from the html and it has the id as filter_search, still I'm getting the error no such element.

Andrei Suvorkov :

Try this code:

WebDriverWait wait = new WebDriverWait(driver,10);
driver.get("http://automation.cloudaccess.host/administrator/index.php?option=com_contact&view=contacts");
driver.findElement(By.id("mod-login-username")).sendKeys("admin");
driver.findElement(By.id("mod-login-password")).sendKeys("admin@123");
driver.findElement(By.id("mod-login-password")).submit();
driver.findElement(By.xpath("//button[@onclick=\"Joomla.submitbutton('contact.add');\"]")).click();
driver.findElement(By.id("jform_name")).sendKeys("James");
driver.findElement(By.xpath("//div[@class='input-append']/a")).click();
driver.switchTo().frame("field-user-modal");
Thread.sleep(5000);
WebElement filter = driver.findElement(By.xpath("//input[@id=\"filter_search\"]"));
filter.click();
filter.sendKeys("admin");
driver.findElement(By.xpath("//button[@type='submit']")).click();
driver.findElement(By.linkText("admin")).click();
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("body > div.modal-backdrop.fade.in")));
driver.findElement(By.xpath("//*[@id='toolbar-apply']/button/span")).click();

it always worth it to add a waits in your test, because Java execution is very quick and the webpage does not. That's why we have to wait until DOM will be completely ready.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=108509&siteId=1