NoSuchElementException:使用给定的搜索参数无法在页面上找到元素

时间:2019-04-09 09:44:09

标签: appium hybrid-mobile-app appium-ios

我正在使用的代码:

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));
}

}

我得到的错误:

  

org.openqa.selenium.NoSuchElementException:使用给定的搜索参数无法在页面上找到元素。   有关此错误的文档,请访问:https://www.seleniumhq.org/exceptions/no_such_element.html   构建信息:版本:'3.141.59',修订版本:'e82be7d358',时间:'2018-11-14T08:17:03'   系统信息:主机:'xxxx.local',ip:'xxxxx',操作系统名称:'Mac OS X',os.arch:'x86_64',os.version:'10 .14.4',java.version:' 1.8.0_152版本”   驱动程序信息:io.appium.java_client.ios.IOSDriver   功能{app:xxx,automationName:XCUITest,browserName:,databaseEnabled:false,deviceName:xxxx,javascriptEnabled:true,locationContextEnabled:false,networkConnectionEnabled:false,平台:MAC,platformName:iOS,platformVersion:12.1,takeScreenshot:true,udid :xxxx ...,webStorageEnabled:false}   会话ID:a2b7627f-9792-4df3-874b-96ced13fc185   ***元素信息:{Using = name,value = loginSettingsButton}   在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)处       在sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)       在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)       在java.lang.reflect.Constructor.newInstance(Constructor.java:423)       在org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)       在org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)       在org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)       在org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)       在io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:239)       在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)       在io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)       在io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)       在io.appium.java_client.ios.IOSDriver.execute(IOSDriver.java:1)       在org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)       在io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:62)       在io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)       在io.appium.java_client.ios.IOSDriver.findElement(IOSDriver.java:1)       在org.openqa.selenium.remote.RemoteWebDriver.findElementByName(RemoteWebDriver.java:404)       在io.appium.java_client.DefaultGenericMobileDriver.findElementByName(DefaultGenericMobileDriver.java:118)       在io.appium.java_client.AppiumDriver.findElementByName(AppiumDriver.java:1)       在io.appium.java_client.ios.IOSDriver.findElementByName(IOSDriver.java:1)       在org.openqa.selenium.By $ ByName.findElement(By.java:284)       在org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)       在io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:58)       在io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)       在io.appium.java_client.ios.IOSDriver.findElement(IOSDriver.java:1)       在com.example.appiumapplication.SafariTest.testCase(SafariTest.java:63)       在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处       在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)       在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       在java.lang.reflect.Method.invoke(Method.java:498)       在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)       在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)       在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)       在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)       在org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)       在org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)       在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)       在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)       在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)       在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)       在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)       在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)       在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)       在org.junit.runners.ParentRunner上$ 2.evaluate(ParentRunner.java:268)       在org.junit.runners.ParentRunner.run(ParentRunner.java:363)       在org.junit.runner.JUnitCore.run(JUnitCore.java:137)       在com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)       在com.intellij.rt.execution.junit.IdeaTestRunner $ Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)       在com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)       在com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)   进程结束,退出代码为255

1 个答案:

答案 0 :(得分:0)

而不是使用 findElement(By.Name(“ ...”))方法,请尝试使用 findElementByAccessibilityId(“ loginSettingsButton”)

loginSettingsButton = iOSDriver.findElementByAccessibiltyId("loginSettingsButton");
  

确保您的元素带有 loginSettingsButton 作为 automationId ,即内容描述

     

使用名称,不建议将xpath用于appium。使用appium时必须使用可访问性ID或id。如果您没有诸如cont-desc,resource-id或id之类的自动化ID,则可以要求您的开发人员团队添加