2015-08-17 59 views
0

我目前正在使用Appium,並且遇到了有關查找WebView上下文的問題。使用UIAutomator我可以檢查WebView並查看其子元素,但是我無法通過使用CSS選擇器/ XPATH訪問這些元素,因爲我無法切換到WebView。Android和Appium(Java):WebView無法訪問

我試過循環過driver.getContextHandles(),但是這只是返回「NAVTIVE_APP」。

正在使用的Android版本是5.0.1和最新版本的Appium。

有誰知道一種方法來找出WebView的名稱或可能切換到它?

+0

開發人員是否啓用WebViews調試?檢查他們是否在代碼中調用了setWebContentsDebuggingEnabled。 – katie

+0

我已經想出瞭解決這個問題的工作方案。而不是試圖通過Name使用的ID收集元素,而是返回我們嘗試訪問的元素。謝謝。 – Richard

回答

0

您需要處理上下文....以下是處理使用Facebook webview登錄的示例。它爲我工作....

Set<String> contextHandles = driver.getContextHandles(); 
    for (String s : contextHandles) { 
     System.out.println("Context : " + s); 
//if context contains webview then set the webview context 
     if (s.contains("WEBVIEW")) { 

      //System.out.println("Set the Context NATIVE to WEBVIEW"); 
      driver.context(s); 
     } 
    } 

    driver.findElement(By.xpath("//android.widget.EditText[@index='0']")).sendKeys(EMAIL); 
    //System.out.println("UserName Entered"); 
    //sleep(3000); 

    driver.findElement(By.xpath("//android.widget.EditText[@index='1']")).sendKeys(PASSWORD); 
    //System.out.println("Password Entered");