Search code examples
appiumhybrid-mobile-appappium-ios

NoSuchElementException: An element could not be located on the page using the given search parameters


Code i am using:

public class SafariTest {
private static IOSDriver iOSDriver;
private WebElement loginSettingsButton;
@Before
public void preTest() throws MalformedURLException {
    DesiredCapabilities capabilities = new DesiredCapabilities();

    capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "xxxx");
    capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");
    capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "12.1");

    capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "XCUITest");
    capabilities.setCapability(MobileCapabilityType.UDID, "xxxx");
    capabilities.setCapability(MobileCapabilityType.APP,"/xxx.app");

    iOSDriver = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

    //webDriver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
@Test
public void testCase(){
    WebDriverWait wait = new WebDriverWait(iOSDriver, 15); 
loginSettingsButton = iOSDriver.findElement(By.name("loginSettingsButton"));
loginSettingsButton.click();
}

@After
public void finishTest(){
    //  webDriver.quit();
}

public static void setContextToWebview(){
    Set<String> availableContexts = iOSDriver.getContextHandles();
    availableContexts.stream()
            .filter(context -> context.toLowerCase().contains("webview"))
            .forEach(newcontext -> iOSDriver.context(newcontext));
}

}

The error which i got:

org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03' System info: host: 'xxxx.local', ip: 'xxxxx', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.14.4', java.version: '1.8.0_152-release' Driver info: io.appium.java_client.ios.IOSDriver Capabilities {app: xxx, automationName: XCUITest, browserName: , databaseEnabled: false, deviceName: xxxx, javascriptEnabled: true, locationContextEnabled: false, networkConnectionEnabled: false, platform: MAC, platformName: iOS, platformVersion: 12.1, takesScreenshot: true, udid: xxxx..., webStorageEnabled: false} Session ID: a2b7627f-9792-4df3-874b-96ced13fc185 *** Element info: {Using=name, value=loginSettingsButton} 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.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158) at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:239) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552) at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42) at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1) at io.appium.java_client.ios.IOSDriver.execute(IOSDriver.java:1) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323) at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:62) at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1) at io.appium.java_client.ios.IOSDriver.findElement(IOSDriver.java:1) at org.openqa.selenium.remote.RemoteWebDriver.findElementByName(RemoteWebDriver.java:404) at io.appium.java_client.DefaultGenericMobileDriver.findElementByName(DefaultGenericMobileDriver.java:118) at io.appium.java_client.AppiumDriver.findElementByName(AppiumDriver.java:1) at io.appium.java_client.ios.IOSDriver.findElementByName(IOSDriver.java:1) at org.openqa.selenium.By$ByName.findElement(By.java:284) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315) at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:58) at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1) at io.appium.java_client.ios.IOSDriver.findElement(IOSDriver.java:1) at com.example.appiumapplication.SafariTest.testCase(SafariTest.java:63) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) Process finished with exit code 255


Solution

  • Instead of using findElement(By.Name("...")) method please try to use findElementByAccessibilityId("loginSettingsButton")

    loginSettingsButton = iOSDriver.findElementByAccessibiltyId("loginSettingsButton");
    

    make sure you have element present with loginSettingsButton as automationId i.e content description

    Using name, xpath is not recommended for appium.You must use accessibilty id or id while using appium. If you don't have automation id like cont-desc, resource-id or id, you can ask your developer team to add it