org.openqa.selenium.NoSuchElementException Appium 与 webview 上下文

问题描述 投票:0回答:1

我们正在使用 Appium for Android 应用程序创建自动测试。我们的应用程序有一些网页视图页面。 重定向到 webview 页面后 - 我更改了上下文,但仍然无法使用 webview 元素。

    @test
    @DisplayName("DAF-T14 Check feedback form after clicking it from profile page")
    public void ProfileAppFeedbackFlow() throws Exception {

    String expectedFeedbackTitle = "Feedback";
    String expectedFeedbackWebTitle = "What are your experiences with the DKV app?";
    String expectedDescription = "You have noticed incorrect station information? Please repor";

    var onboardingPage = new OnboardingPage();
    var onboardingLinearLayout = onboardingPage.clickSkipButton();
    var locationPopUp = onboardingLinearLayout.clickLaterButton();
    var analysisPopUp = locationPopUp.clickAllowAllTimeButton();
    analysisPopUp.clickActivateButton();
    var feedbackProfilePage = new DoYouLikePopUp().clickNoButton();
    Assertions.assertEquals(feedbackProfilePage.getFeedbackTittle(), expectedFeedbackTitle);

现在我正在检查并将上下文更改为 webview

    Set strcontext = null;
    try{
    strcontext = ((AndroidDriver) AppiumInit.getAppiumDriver()).getContextHandles();
    System.out.println("Found contexts size: '" + strcontext.size() + "'");
    for (String s :strcontext) {
    System.out.println("Found context: '" + s + "'");
    if (s != null && s.contains("WEBVIEW")) {
    System.out.println("Switching context to: '" + s + "'");
    ((AndroidDriver) AppiumInit.getAppiumDriver()).context(s);
    break;
    }
    }
    } catch (Exception e){
    e.printStackTrace();
    }

检查上下文是否为webview

    String currentContext = ((AndroidDriver) AppiumInit.getAppiumDriver()).getContext();
    System.out.println("Current Context: " + currentContext);

并检查标题文本

   Assertions.assertEquals(feedbackProfilePage.getFeedbackWebTitl(), expectedFeedbackWebTitle); 

哪里

    public String getFeedbackWebTitl() {
    WebElement feedbackWebTitle = driver.findElement(By.xpath("//span[@id='title']"));
    return feedbackWebTitle.getText();
    }

我可以在日志中看到什么:

Found contexts size: '2'
Found context: 'NATIVE_APP'
Found context: 'WEBVIEW_com.dkveuroservice.mobileappkit'
Switching context to: 'WEBVIEW_com.dkveuroservice.mobileappkit'
Current Context: WEBVIEW_com.dkveuroservice.mobileappkit
<html><head></head><body></body></html>

org.openqa.selenium.NoSuchElementException:没有这样的元素:无法定位元素:{“method”:“xpath”,“selector”:“//span[@id='title']”} (会话信息:chrome=103.0.5060.71) 有关此错误的文档,请访问:https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception 构建信息:版本:'4.10.0',修订版:'c14d967899' 系统信息:os.name:'Mac OS X',os.arch:'aarch64',os.version:'13.5.1',java.version:'17.0.6' 驱动程序信息:io.appium.java_client.android.AndroidDriver 命令:[629bae0e-84be-463f-86a7-49e1c58ddbcb, findElement {using=xpath, value=//span[@id='title']}] 功能 {appium:app://Users//nifargo//Documents..., appium:appActivity: com.dkveuroservice.mobileap..., appium:appPackage: com.dkveuroservice.mobileap..., appium:automationName: UiAutomator2 , appium:databaseEnabled: false, appium:desired: {app: //Users//nifargo//Documents..., appActivity: com.dkveuroservice.mobileap..., appPackage: com.dkveuroservice.mobileap..., AutomationName :UiAutomator2,deviceName:autoTests,newCommandTimeout:800,platformName:ANDROID},appium:deviceApiLevel:33,appium:deviceManufacturer:Google,appium:deviceModel:sdk_gphone64_arm64,appium:deviceName:emulator-5554,appium:deviceScreenDensity:560,appium: deviceScreenSize:1440x2560,appium:deviceUDID:emulator-5554,appium:javascriptEnabled:true,appium:locationContextEnabled:false,appium:networkConnectionEnabled:true,appium:newCommandTimeout:800,appium:pixelRatio:3.5,appium:platformVersion:13,appium: statBarHeight:84,appium:takesScreenshot:true,appium:viewportRect:{高度:2392,左:0,顶部:84,宽度:1440},appium:警告:{},appium:webStorageEnabled:假,platformName:ANDROID} 会话 ID:629bae0e-84be-463f-86a7-49e1c58ddbcb 改变了

日志 https://gist.github.com/Nifargo/2920de27ba7ea76625f7430a6bf6bf96

期望我可以检查网页元素的文本

android webview appium native nosuchelementexception
1个回答
0
投票

谢谢,您解决了这个问题吗?如何解决的?面临同样的问题,有时它找到元素,有时找不到

© www.soinside.com 2019 - 2024. All rights reserved.