Cannot Find Element

Appium version 1.7.1
Having trouble finding elements in via iOS Simulator. I have tried find element by class name, identifier, xpath, and accessibility label. Non of these seem to work, yet I can see all of these elements using the appium inspector. Has anyone dealt with a similar issue?

Running tests with command ./gradlew test

Code with tests:

package tests.localDevices;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.remote.IOSMobileCapabilityType;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.ios.IOSElement;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.By;
import org.testng.annotations.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.logging.Level;

public class iOSClientShellBanners {
    private String host = "localhost";
    private int port = 4723;
    private String reportDirectory = "reports";
    private String reportFormat = "xml";
    private String deviceName = "iPhone 8";
    private String udid = "7B721CF8-C1E7-4010-968B-BF2984B5AA75";
    private String automationName = "XCUITest";
    private String platformName = "iOS";
    private String testName = "ios-clientshell-banners";
    protected IOSDriver<IOSElement> driver = null;
    DesiredCapabilities dc = new DesiredCapabilities();

    @BeforeMethod
    public void setUp() throws MalformedURLException {
        dc.setCapability("reportDirectory", reportDirectory);
        dc.setCapability("reportFormat", reportFormat);
        dc.setCapability("testName", testName);
        dc.setCapability("deviceName", deviceName);
        dc.setCapability("udid", udid);
        dc.setCapability("automationName", automationName);
        dc.setCapability("platformName", platformName);
        dc.setCapability(IOSMobileCapabilityType.BUNDLE_ID, "com.gpshopperenterprise.clientshell");
        driver = new IOSDriver<>(new URL("http://" + host + ":" + port + "/wd/hub"), dc);
        driver.setLogLevel(Level.INFO);
    }

    @Test
    public void testiOSClientShellBanners() {
        sleepForABit();
        clickElementByXPath("(//*[@accessibilityLabel='home_carousel_1'])[1]");
        sleepForABit();
        clickElementByID("home_carousel_1");
        clickElementByClassName("GDKBannerView");
        sleepForABit();
        clickElementByXPath("//*[@class='_UIBackButtonContainerView']");
        sleepForABit();
        tapHomeTab();
        sleepForABit();
        driver.swipe(500, 552, 80, 533, 2370);
        clickElementByXPath("//*[@accessibilityLabel='home_carousel_2']");
        sleepForABit();
        clickElementByID("home_carousel_2");
        sleepForABit();
        tapHomeTab();
        sleepForABit();
        driver.swipe(533, 518, 106, 526, 505);
        clickElementByXPath("//*[@accessibilityLabel='home_carousel_3']");
        sleepForABit();
        clickElementByID("home_carousel_3");
        sleepForABit();
        clickElementByXPath("//*[@class='_UIBackButtonContainerView']");
        sleepForABit();
        driver.swipe(307, 1012, 295, 596, 386);
    }

    public void tapHomeTab() {
        clickElementByClassName("UITabBarButton");
    }

    public void clickElementByClassName(String className) {
        driver.findElementByClassName(className).click();
    }

    public void clickElementByID(String identifier) {
        driver.findElementByAccessibilityId(identifier).click();
    }

    public void clickElementByName(String name) {
        driver.findElement(By.name(name)).click();
    }

    public void clickElementByXPath(String path) {
        driver.findElementByXPath(path).click();
    }

    public void sleepForABit() {
        try{Thread.sleep(1000);} catch(Exception ignore){}   
    }

    @AfterMethod
    public void tearDown() {
        driver.quit();
    }
}

Appium Output - Failure finding element by class name:

[debug] [MJSONWP] Calling AppiumDriver.findElement() with args: ["class name","UITabBarButton","62221ddf-ba70-44fc-829f-0b64baa02a36"]
[debug] [XCUITest] Executing command 'findElement'
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, name, class name, -ios predicate string, -ios class chain, accessibility id
[XCUITest] Rewrote incoming selector from 'UITabBarButton' to 'XCUIElementTypeUITabBarButton' to match XCUI type. You should consider updating your tests to use the new selectors directly
[debug] [BaseDriver] Waiting up to 0 ms for condition
[debug] [JSONWP Proxy] Proxying [POST /element] to [POST http://localhost:8100/session/0593C6CC-BCF2-4E20-95BE-8D4A4A6EB212/element] with body: {"using":"class name","value":"XCUIElementTypeUITabBarButton"}
[debug] [JSONWP Proxy] Got response with status 200: {"value":"Invalid argument for class used 'XCUIElementTypeUITabBarButton'. Did you mean XCUIElementTypeXCUIElementTypeUITabBarButton?","sessionId":"0593C6CC-BCF2-4E20-95BE-8D4A4A6EB212","status":15}

Appium Output - Failure finding element with accessibility id:

[debug] [MJSONWP] Calling AppiumDriver.findElement() with args: ["accessibility id","home_carousel_2","c4794223-5be8-4035-b5d5-e876aaa572f6"]
[debug] [XCUITest] Executing command 'findElement'
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, name, class name, -ios predicate string, -ios class chain, accessibility id
[debug] [BaseDriver] Waiting up to 0 ms for condition
[debug] [JSONWP Proxy] Proxying [POST /element] to [POST http://localhost:8100/session/C4641A12-3258-4BD0-BB32-65EEB1452FD5/element] with body: {"using":"accessibility id","value":"home_carousel_2"}
[debug] [JSONWP Proxy] Got response with status 200: {"value":{"using":"accessibility id","value":"home_carousel_2","description":"unable to find an element"},"sessionId":"C4641A12-3258-4BD0-BB32-65EEB1452FD5","status":7}
[HTTP] <-- POST /wd/hub/session/c4794223-5be8-4035-b5d5-e876aaa572f6/element 500 161 ms - 164 
[HTTP] --> DELETE /wd/hub/session/c4794223-5be8-4035-b5d5-e876aaa572f6 {}

Appium Inspector:

@anthonyfennell : Hey Anthony ! Could you give some details of the IOS Simulator version and App you’re testing (if you can). Also please post your code with all details.

Simulator is the iPhone 8 running iOS 11.2
I updated the post with my code. Let me know if anything else would be helpful to debug this issue.

Can you also post the screenshot of Appium inspector too ?

Surely. Just updated the post with a screen shot of the inspector.

@anthonyfennell: Hey ! Thanks for the screenshot :slight_smile: !

Can you tell now what exactly you’re doing in here. Are you trying to click on the image or the bottomTypeBarButtons ?

As a first step, just clicking the “Home” tab button (Or as you say bottomTypeBarButtons). As an end result I will be clicking everything on this screen.

@anthonyfennell: I think I’ve understood your issue.

I observe that the classname is wrong. Why don’t try this way :

1 Like

Using your implementation for tapping the home button works:

driver.findElementsByClassName("XCUIElementTypeButton").get(0).click();

Is there documentation on these method calls for the (IOSDriver) I will also need to find an element by its text value. Which I assume is the call

driver.findElementByName("insertNameHere")
Well I'm not so sure of the documentation. But I think you can find it in their git-repo. 
Correct !!!
1 Like