Selenium Actions moveToElement not working for browserstack iphone only

170 Views Asked by At

I'm writing a simple selenium automation for a Angular single page applcation and integrated the browserstack. This code run fine in locally, and works on browserstack for below platforms.

  - os: OS X
    osVersion: Big Sur
    browserName: Safari
    browserVersion: 14.1
  - os: Windows
    osVersion: 10
    browserName: Edge
    browserVersion: latest
  - deviceName: Samsung Galaxy S22 Ultra
    browserName: chrome # Try 'samsung' for Samsung browser
    osVersion: 12.0
  - deviceName: Google Pixel 7
    osVersion: 13.0
    browserName: chrome
    deviceOrientation: landscape

But not working for iphones.

  - deviceName: iPhone 14
    osVersion: 16
    browserName: safari
    deviceOrientation: portrait

This is the code.

var act = new Actions(driver);
var target = driver.findElement(By.cssSelector("[aria-label=\"nav-search-btn\"]"));
target.click();
act.moveToElement(target).build().perform();

Error for iphone is

The element identified by "5001" is either not present or it has expired from the internal cache. Try to find it again For documentation on this error, please visit: https://selenium.dev/exceptions/#stale_element_reference Build info: 

It says element 5001 not present or expired. But it cannot be happenned because I could click it in previous line.

I could capture the internal API calls that sends to browserstack also.

This is for click, this works fine.

POST /session/xxx/element/5002/click {
  "id": "5002"
}

This is for the moveToElement.

POST /session/xxx/actions -- {
  "actions": [
    {
      "id": "default mouse",
      "type": "pointer",
      "parameters": {
        "pointerType": "mouse"
      },
      "actions": [
        {
          "duration": 100,
          "x": 0,
          "y": 0,
          "type": "pointerMove",
          "origin": {
            "ELEMENT": "5002",
            "element-6066-11e4-a52e-4f735466cecf": "5002"
          }
        }
      ]
    }
  ]
}

Seems the issue only occurs for moveToElement only and for iphones only.

1

There are 1 best solutions below

1
On

Instead of using the moveToElement please try to incorporate the mobile:scroll method:

RemoteWebElement element = (RemoteWebElement)driver.findElement(By.className("XCUIElementTypeTable"));
String elementID = element.getId();
HashMap<String, String> scrollObject = new HashMap<String, String>();
scrollObject.put("element", elementID); // Only for ‘scroll in element’
scrollObject.put("direction", "down");
driver.executeScript("mobile:scroll", scrollObject);

I hope this helps you.

Thanks.