当前位置: 首页 > 知识库问答 >
问题:

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

耿珂
2023-03-14

我正在使用的代码:

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

}

我得到的错误:

共有1个答案

诸葛雨泽
2023-03-14

而不是使用findElement(by.name(“...”))方法请尝试使用findElementByAccessibilityId(“LoginSettingsButton”)

loginSettingsButton = iOSDriver.findElementByAccessibiltyId("loginSettingsButton");

确保将loginSettingsButton中的元素作为automationId(即内容描述)呈现

对于Appium,xpath不建议使用name。在使用Appium时,必须使用accessibilty id或id。如果您没有像cont-desc、resource-id或id这样的自动化id,您可以要求您的开发团队添加它

 类似资料: