Appium java:无法使用 android studio 模拟器在 webview 中找到元素

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

我一直在尝试访问 webview 中的元素,但 appium 找不到它。在下面的脚本中,我要做的是在 WebView 上打开“Amazon.com”,然后在搜索栏中搜索“Iphone”一词。 我将驱动程序切换到 webview,添加了一个明确的等待,元素 cssSelector 是正确的,我尝试使用 xpath 和 Id,但我得到了同样的错误:

org.openqa.selenium.NoSuchElementException:没有这样的元素:无法定位元素:{“方法”:“css选择器”,“选择器”:“#twotabsearchtextbox”} (会话信息:chrome=83.0.4103.106)

这是代码,提前谢谢你

public class WebViewSimpleTest {


public AndroidDriver driver;
public Set<String> contextNames;
public WebDriverWait wait;

@Before
public void setUp() throws Exception {
    String appiumServerURL = "http://127.0.0.1:4723/wd/hub";
    
    DesiredCapabilities dc = new DesiredCapabilities();
    
    dc.setCapability("platformName", "Android");
    dc.setCapability("platformVersion", "11.0");
    dc.setCapability("deviceName", "Android Emulator");
    dc.setCapability("automationName", "UiAutomator2");
    dc.setCapability("appium:appPackage", "org.chromium.webview_shell");
    dc.setCapability("appium:appActivity", "org.chromium.webview_shell.WebViewBrowserActivity");
    dc.setCapability("appium:ensureWebviewsHavePages", true);
    dc.setCapability("appium:nativeWebScreenshot", true);
    dc.setCapability("appium:newCommandTimeout", 3600);
    dc.setCapability("appium:connectHardwareKeyboard", true);
    dc.setCapability("appium:recreateChromeDriverSessions", true);
    
    driver = new AndroidDriver(new URL(appiumServerURL), dc);
    wait = new WebDriverWait(driver, 10);
    contextNames = driver.getContextHandles();
    for (String contextName : contextNames) {
        System.out.println(contextName); 
    }
    
}

    @After
    public void tearDown() throws Exception {
        driver.context("NATIVE_APP");
        driver.quit();
    }

    @Test
    public void testToSearchForWord() throws InterruptedException {
        
        driver.get("https://www.amazon.com/");
        
         //switch driver
        driver.context((String) contextNames.toArray()[1]);
        //driver.context("WEBVIEW_org.chromium.webview_shell");
        
        //find the search bar
        MobileElement searchBar = (MobileElement) wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#twotabsearchtextbox")));
        searchBar.sendKeys("iphone");


        MobileElement searchButton = (MobileElement) driver.findElement(By.cssSelector("#nav-search-submit-button"));
        searchButton.click();

        // Wait for the results to show up
        Thread.sleep(5000);

        
}

}

Appium v1.22.3; 安卓工作室 v11.0.15; java 客户端 v7.6.0

android-studio webview android-emulator appium-android appium-java
© www.soinside.com 2019 - 2024. All rights reserved.