appium混合应用程序错误org.openqa.selenium.nosuchelementexception:找不到元素

fcg9iug3  于 2021-07-07  发布在  Java
关注(0)|答案(0)|浏览(265)

appium hybrid app testing抛出“org.openqa.selenium.nosuchelementexception:无法定位元素”错误。
环境:intellij chrome:版本87.0.4280.66应用程序:版本1.18.3

appium源代码如下:

package com.example.appium;
import org.openqa.selenium.remote.CapabilityType;
import org.testng.Assert;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class HybridApp {
    //create Android driver object
    public static AndroidDriver<MobileElement> driver;
    public static WebDriverWait wait;
    @Test
    @Parameters("runOn")
    public static void test(@Optional("ZH33L2Z6KL") String runOn) throws MalformedURLException, InterruptedException {
        //create By object for Webview - android.webkit.WebView
        By webView = By.className("android.webkit.WebView");
        //create Android driver object
        //Initialize the test
        // Created object of DesiredCapabilities class.
        DesiredCapabilities capabilities = new DesiredCapabilities();
        if (runOn.contains("ZH33L2Z6KL")) {
            capabilities.setCapability("udid", "ZH33L2Z6KL");
            capabilities.setCapability("deviceName", "Moto G");
            capabilities.setCapability("systemPort", "8201");
            // Set android platformName desired capability. It's Android in our case here.
            capabilities.setCapability("platformName", "Android");
            capabilities.setCapability("platformVersion", "7.1.1");

        } else if (runOn.contains("fca3752eeaac")) {
            capabilities.setCapability("udid", "fca3752eeaac");
            capabilities.setCapability("deviceName", "MI A3");
            capabilities.setCapability("systemPort", "8202");
            // Set android platformName desired capability. It's Android in our case here.
            capabilities.setCapability("platformName", "Android");
            capabilities.setCapability("platformVersion", "10");
        }
        capabilities.setCapability("automationName", "uiautomator2");
        // Set app path
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
        capabilities.setCapability("chromedriverExecutable","C:\\chromedriver\\chromedriver.exe");
        capabilities.setCapability("app","D:\\workshop\\Appium\\backup\\WebView-Test.apk");
        // Created object of RemoteWebDriver will all set capabilities.
        // Set appium server address and port number in URL string.
        capabilities.setCapability("skipUnlock","true");
        capabilities.setCapability("autoGrantPermissions","true");
        capabilities.setCapability("noReset","false");
        // It will launch Chrome app in android device.
        driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        wait = new WebDriverWait(driver, 300);
        //1. Native app part
        //By Id
        //Locate "go website" using its ID and click
        wait.until(ExpectedConditions.visibilityOfElementLocated
                (By.id("com.snc.test.webview2:id/action_go_website"))).click();
        //Allow app permissions
        allowAppPermission();
        //Locate "input URL" field using its ID and type "www.facebook.com"
        wait.until(ExpectedConditions.visibilityOfElementLocated
                (By.id("com.snc.test.webview2:id/input_url")));
        WebElement inputurl = driver.findElement(By.id("com.snc.test.webview2:id/input_url"));
        inputurl.clear();
        inputurl.sendKeys("m.facebook.com");
        //Locate Move button using its name and click
        WebElement movebutton = driver.findElementById("android:id/button1");
        movebutton.click();
        Thread.sleep(5000);
        //Context switch to Webview
        //Create wait object for Webdriver wait
        WebDriverWait wait = new WebDriverWait(driver,300);
        // Wait till Webview element appears
        wait.until(ExpectedConditions.visibilityOfElementLocated(webView));
        // Call getContext() method to return contexts like 'NATIVE_APP' or 'WEBVIEW_1'
        Set<String> availableContexts = driver.getContextHandles();
        System.out.println("Total No of Context  = "+ availableContexts.size());
        //Loop through contexts and select Webview
        for(String context : availableContexts) {
            if(context.contains("WEBVIEW")){
                System.out.println("Context Name is " + context);
//                break;
            }
        }
        System.out.println(driver.getPageSource());
        //2. Webapp part
        //By Id
        //Locate Email and Password fields using their Ids
        Thread.sleep(5000);
        WebElement email = driver.findElementById("com.snc.test.webview2:id/m_login_email");
//        WebElement email = driver.findElementById("m_login_email");
        email.clear();
        email.sendKeys("test@gmail.com");
        //By name
        //Locate Log In button using its name and click
        WebElement loginbutton = driver.findElementByName("com.snc.test.webview2:name/login");
        loginbutton.click();
        WebElement password = driver.findElementById("com.snc.test.webview2:id/m_login_password");
        password.clear();
        password.sendKeys("test123");
        //By name
        //Locate Log In button using its name and Log In
        loginbutton = driver.findElementByName("com.snc.test.webview2:name/login");
        loginbutton.click();
        Thread.sleep(15000);
        //By XPath
        //Locate Error message using XPath and get the message using 'getText' method
        //WebElement errormessage = driver.findElementByXPath("//*[@class='_52jd']/span");
        wait.until(ExpectedConditions.visibilityOfElementLocated
                (By.xpath("//div[@id='root']//span")));
        WebElement errormessage = driver.findElementByXPath("//div[@id='root']//span");
        String message = errormessage.getText();
        System.out.println("Error message = "+message);
        //Assert Error message
        Assert.assertEquals(
                "The password you entered is incorrect. Did you forget your password?", message);
    }

    public static void allowAppPermission() {
        try{
            //In a loop, check for window with 2 buttons and one button is having name as "Allow"
            while (driver.findElementsByXPath("//*[@class='android.widget.Button'][2]").size() > 0 &&
                    driver.findElementById("com.android.packageinstaller:id/permission_allow_button")
                            .getText().contains("ALLOW"))
//                             .getText().contains("Allow"))
            {
                //Click allow button
                //com.android.packageinstaller:id/permission_allow_button
                driver.findElementByXPath("//*[@class='android.widget.Button'][2]").click();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

testng.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Regression" thread-count="2" parallel="tests" preserve-order="true" verbose="2">
   <test name="Real Device1">
        <classes>
            <class name="com.example.appium.HybridApp"/>
        </classes>
    </test>
</suite>

build.gradle如下:

plugins {
    id "application"
}
group 'io.cloudgrey.appiumpro'
//version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    maven { url "https://jitpack.io" }
    mavenCentral()
    maven { url "https://repo.maven.apache.org/maven2" }
}

dependencies {
    implementation 'org.testng:testng:6.14.3'
    implementation 'junit:junit:4.12'
    testCompile group: 'org.testng', name: 'testng', version: '6.14.3'
    compile 'log4j:log4j:1.2.17'
    testCompile group: 'log4j', name: 'log4j', version:'1.2.17'
    testCompile group: 'io.appium', name: 'java-client', version: '7.3.0'
    testCompile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.13.0'
    testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '1.3'
    testCompile group: 'com.eclipsesource.minimal-json', name: 'minimal-json', version: '0.9.5'
    testCompile group: 'org.java-websocket', name: 'Java-WebSocket', version: '1.3.9'
    testCompile group: 'org.zeroturnaround', name: 'zt-exec', version: '1.10'
    testCompile group: 'me.xdrop', name: 'fuzzywuzzy', version: '1.2.0'
    testCompile group: 'io.appium', name: 'mitmproxy-java', version: '1.6.1'
    testCompile group: 'com.applitools', name: 'eyes-appium-java4', version: '4.2.1'
    testCompile group: 'com.github.testdotai', name: 'classifier-client-java', version: '1.0.0'
    testCompile group: 'com.google.guava', name: 'guava', version: '28.1-jre'
}

执行testng.xml时,会抛出以下错误:

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: 'DESKTOP-E205GQ8', ip: '192.168.0.105', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_261'
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities {app: D:\workshop\Appium\backup\W..., appPackage: com.snc.test.webview2, autoGrantPermissions: true, automationName: uiautomator2, browserName: , chromedriverExecutable: C:\chromedriver\chromedrive..., databaseEnabled: false, desired: {app: D:\workshop\Appium\backup\W..., autoGrantPermissions: true, automationName: uiautomator2, browserName: , chromedriverExecutable: C:\chromedriver\chromedrive..., deviceName: Moto G, noReset: false, platformName: android, platformVersion: 7.1.1, skipUnlock: true, systemPort: 8201, udid: ZH33L2Z6KL}, deviceApiLevel: 25, deviceManufacturer: motorola, deviceModel: Moto E (4) Plus, deviceName: ZH33L2Z6KL, deviceScreenDensity: 320, deviceScreenSize: 720x1280, deviceUDID: ZH33L2Z6KL, javascriptEnabled: true, locationContextEnabled: false, networkConnectionEnabled: true, noReset: false, pixelRatio: 2, platform: LINUX, platformName: Android, platformVersion: 7.1.1, skipUnlock: true, statBarHeight: 48, systemPort: 8201, takesScreenshot: true, udid: ZH33L2Z6KL, viewportRect: {height: 1136, left: 0, top: 48, width: 720}, warnings: {}, webStorageEnabled: false}
Session ID: 66cdba3f-a6cf-47b1-a36a-306abe1ada3d

***Element info: {Using=id, value=com.snc.test.webview2:id/m_login_email}

    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:41)
    at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
    at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:61)
    at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.findElement(AndroidDriver.java:1)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:372)
    at io.appium.java_client.DefaultGenericMobileDriver.findElementById(DefaultGenericMobileDriver.java:69)
    at io.appium.java_client.AppiumDriver.findElementById(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.findElementById(AndroidDriver.java:1)
    at com.example.appium.HybridApp.test(HybridApp.java:98)
    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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.access$000(SuiteRunner.java:40)
    at org.testng.SuiteRunner$SuiteWorker.run(SuiteRunner.java:489)
    at org.testng.internal.thread.ThreadUtil$1.call(ThreadUtil.java:52)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

问题:
为什么webview元素-“com.snc.test.webview2:id/m\u login\u email”没有标识?
这个问题是否是因为chrome不兼容?
这个问题是否是因为appium 1.18.3?
请提供此问题的解决方案。
注:appium日志以图像形式给出

打开facebook.com后的页面来源为图片:

facebook页面截图附为图片:

之后chrome://inspect/#devices,html如下所示

<div class="_5rut" data-sigil="login_inner"><form method="post" action="/login/device-based/login/async/?refsrc=https%3A%2F%2Fm.facebook.com%2F&amp;lwv=100" class="mobile-login-form _9hp- _5spm" id="login_form" novalidate="1" data-sigil="m_login_form" data-autoid="autoid_4"><input type="hidden" name="lsd" value="AVrnNzENc_Y" autocomplete="off"><input type="hidden" name="jazoest" value="21005" autocomplete="off"><input type="hidden" name="m_ts" value="1607101644"><input type="hidden" name="li" value="zGzKXwGfdZPQoStdf0pH_RBm"><input type="hidden" name="try_number" value="0" data-sigil="m_login_try_number"><input type="hidden" name="unrecognized_tries" value="0" data-sigil="m_login_unrecognized_tries"><div id="user_info_container" data-sigil="user_info_after_failure_element"></div><div id="pwd_label_container" data-sigil="user_info_after_failure_element"></div><div id="otp_retrieve_desc_container"></div><div><div class="_56be"><div class="_55wo _56bf"><div class="_96n9" id="email_input_container"><input autocorrect="off" autocapitalize="off" type="email" class="_56bg _4u9z _5ruq _8qtn" autocomplete="on" id="m_login_email" name="email" placeholder="ಮೊಬೈಲ್ ಸಂಖ್ಯೆ ಅಥವಾ ಇಮೇಲ್" data-sigil="m_login_email"></div></div></div><div class="_55wq"></div><div class="_56be"><div class="_55wo _56bf"><div class="_1upc _mg8" data-sigil="m_login_password"><div class="_7om2"><div class="_4g34 _5i2i _52we"><div class="_5xu4"><input autocorrect="off" autocapitalize="off" class="_56bg _4u9z _27z2 _8qtm" autocomplete="on" id="m_login_password" name="pass" placeholder="ಪಾಸ್&zwnj;ವರ್ಡ್" type="password" data-sigil="password-plain-text-toggle-input"></div></div><div class="_5s61 _216i _5i2i _52we"><div class="_5xu4"><div class="_2pi9" style="display:none" id="u_0_2"><a href="#" data-sigil="password-plain-text-toggle"><span class="mfss" style="display:none" id="u_0_3">ಮರೆಮಾಡಿ</span><span class="mfss" id="u_0_4">ತೋರಿಸಿ</span></a></div></div></div></div></div></div></div></div><div class="_2pie" style="text-align:center;"><div id="u_0_5" data-sigil="login_password_step_element"><button type="button" value="ಲಾಗ್ ಇನ್ ಮಾಡು" class="_54k8 _52jh _56bs _56b_ _28lf _9cow _56bw _56bu" name="login" data-sigil="touchable login_button_block m_login_button" data-autoid="autoid_3"><span class="_55sr">ಲಾಗ್ ಇನ್ ಮಾಡು</span></button></div><div class="_7eif" id="oauth_login_button_container" style="display:none"></div><div class="_7f_d" id="oauth_login_desc_container" style="display:none"></div><div id="otp_button_elem_container"></div></div><input type="hidden" name="prefill_contact_point" id="prefill_contact_point"><input type="hidden" name="prefill_source" id="prefill_source"><input type="hidden" name="prefill_type" id="prefill_type"><input type="hidden" name="first_prefill_source" id="first_prefill_source"><input type="hidden" name="first_prefill_type" id="first_prefill_type"><input type="hidden" name="had_cp_prefilled" id="had_cp_prefilled" value="false"><input type="hidden" name="had_password_prefilled" id="had_password_prefilled" value="false"><input type="hidden" name="is_smart_lock" id="is_smart_lock" value="false"><div class="_xo8"></div><noscript>&lt;input type="hidden" name="_fb_noscript" value="true" /&gt;</noscript></form><div><div id="login_reg_separator" class="_43mg _8qtf" data-sigil="login_reg_separator"><span class="_43mh">ಅಥವಾ</span></div><div class="_52jj _5t3b" id="signup_button_area"><a role="button" class="_5t3c _28le btn btnS medBtn mfsm touchable" id="signup-button" tabindex="0" data-sigil="m_reg_button" data-autoid="autoid_2">ಹೊಸ ಖಾತೆಯನ್ನು ರಚಿಸಿ</a></div></div><div><div data-sigil="login_identify_step_element"></div><div class="other-links _8p_m"><ul class="_5pkb _55wp"><li><span class="mfss fcg"><a tabindex="0" href="/recover/initiate/?c=https%3A%2F%2Fm.facebook.com%2F&amp;r&amp;cuid&amp;ars=facebook_login&amp;privacy_mutation_token=eyJ0eXBlIjowLCJjcmVhdGlvbl90aW1lIjoxNjA3MTAxNjQ0LCJjYWxsc2l0ZV9pZCI6Mjg0Nzg1MTQ5MzQ1MzY5fQ%3D%3D&amp;lwv=100&amp;refid=8" id="forgot-password-link">ಪಾಸ್&zwnj;ವರ್ಡ್ ಮರೆತಿರುವಿರಾ?</a></span></li></ul></div><div class="other-links _8p_m _9f37" data-sigil="reg_link"></div></div></div>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题