我正在尝试使用 Appium 进行混合应用程序测试。我正在尝试通过 xpath 获取元素。但我面临以下问题。
org.openqa.selenium.WebDriverException: org.openqa.selenium.InvalidArgumentException: invalid argument: invalid locator
(Session info: chrome=115.0.5790.166)
这是我从 Chrome 开发者选项中获取的 html。
<mat-select _ngcontent-anp-c247="" role="combobox" aria-autocomplete="none" aria-haspopup="true" data-mx-id="mx-cb-20101-slt-choosecompany" class="mat-select ng-tns-c85-113 ng-tns-c60-112 ng-pristine ng-valid ng-star-inserted ng-touched" aria-labelledby="mat-form-field-label-45 mat-select-value-33" id="mat-select-32" tabindex="0" aria-expanded="false" aria-required="false" aria-disabled="false" aria-invalid="false" xpath="1"><div cdk-overlay-origin="" class="mat-select-trigger ng-tns-c85-113"><div class="mat-select-value ng-tns-c85-113" id="mat-select-value-33"><!----><span class="mat-select-value-text ng-tns-c85-113 ng-star-inserted" style=""><span class="mat-select-min-line ng-tns-c85-113 ng-star-inserted"> India Pvt Ltd</span><!----><!----></span><!----></div><div class="mat-select-arrow-wrapper ng-tns-c85-113"><div class="mat-select-arrow ng-tns-c85-113"></div></div></div><!----></mat-select>
我创建了如下 xpath:
//*[contains(@data-mx-id,'mx-cb-20101-slt-choosecompany')]
当我将此 xpath 粘贴到 chrome 开发选项中时,元素位置完美显示。
当我尝试使用 Appium 检查器时,元素位置完美显示。
但是当我尝试使用 Appium-Selenium 代码时,我遇到了
invalid argument: invalid locator
异常。
我通过以下代码切换到webview上下文。
Set<String> contextNames = driver.getContextHandles();
for (String context : contextNames) {
if (context.contains("WEBVIEW")) {
System.out.println("Context Name is " + context);
driver.context(context);
break;
}
}
这就是我通过 xpath 获取元素的方式 @AndroidFindBy(xpath ="//*[包含(@data-mx-id,'mx-cb-20101-slt-choosecompany')]") 公共 WebElement testSpinner;
如果我提供完整的 xpath,它就可以正常工作。但我不应该按照 appium 建议使用完整的 xpath。
请帮我解决这个问题。
尝试设置
chromeOptions
功能,如下所示。
对于 Java:
setCapability("chromeOptions", ImmutableMap.of("w3c", false));
对于Python:
chromeOptions={"w3c": False}