user1207289
user1207289

Reputation: 3263

receiving a webElement List - casting error

I have a method like this

  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;

      }

and I am calling AgreeAllow() like this in a different file

List<WebElement>  AgreeAllowHandler =  PageObjectOneInst.AgreeAllow();


      tAction.tap((WebElement) AgreeAllowHandler.get(0));
      tAction.tap((WebElement) AgreeAllowHandler.get(1));
      ...
      ...

Its giving me this error java.util.ArrayList cannot be cast to org.openqa.selenium.WebElement

I tried this also, but couldn't resolve

 List<WebElement>  AgreeAllowHandler =   List<WebElement>  PageObjectOneInst.AgreeAllow();

I have looked at several SO questions. I tried below also from here

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

how do I receive/cast it correctly?

Upvotes: 0

Views: 1333

Answers (1)

Evgeny
Evgeny

Reputation: 2568

findElementsByAndroidUIAutomator returns list of elements. Use findElementByAndroidUIAutomator instead.

 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

Upvotes: 1

Related Questions