收到webElement列表 - 转换错误

时间:2017-10-23 19:04:15

标签: java selenium-webdriver

我有这样的方法

  public List<WebElement> AgreeAllow(){

          WebElement ButtonAgree = (WebElement) driver.findElementsByAndroidUIAutomator(SelectorRepo.AgreeButton);
          WebElement ButtonAllow = (WebElement) driver.findElementsByAndroidUIAutomator(SelectorRepo.AllowButton);

          List<WebElement> AgreeAllowFunction = new ArrayList<WebElement>();

         // ArrayList AgreeAllowFunction = new ArrayList<WebElement>(); 
          AgreeAllowFunction.add(ButtonAgree);
          AgreeAllowFunction.add(ButtonAllow);

          return AgreeAllowFunction;

      }

我在另一个档案中将AgreeAllow()称为List<WebElement> AgreeAllowHandler = PageObjectOneInst.AgreeAllow(); tAction.tap((WebElement) AgreeAllowHandler.get(0)); tAction.tap((WebElement) AgreeAllowHandler.get(1)); ... ...

java.util.ArrayList cannot be cast to org.openqa.selenium.WebElement

它给了我这个错误 List<WebElement> AgreeAllowHandler = List<WebElement> PageObjectOneInst.AgreeAllow();

我也试过了,但无法解决

List<WebElement>  AgreeAllowHandler = new ArrayList<>();      
      for (WebElement wElement : PageObjectOneInst.AgreeAllow()){
          Class <WebElement> c = null;
          WebElement we =  c.cast(wElement);
          AgreeAllowHandler.add(we);
        }

我看过几个SO问题。我也在here

下面尝试过
{{1}}

我如何正确接收/投射?

1 个答案:

答案 0 :(得分:1)

findElementsByAndroidUIAutomator返回元素列表。请改用findElementByAndroidUIAutomator

 public List<WebElement> AgreeAllow(){

      WebElement ButtonAgree = (WebElement) driver.findElementByAndroidUIAutomator(SelectorRepo.AgreeButton);
      WebElement ButtonAllow = (WebElement) driver.findElementByAndroidUIAutomator(SelectorRepo.AllowButton);
      ...
 }

https://appium.github.io/java-client/io/appium/java_client/FindsByAndroidUIAutomator.html