0

i am trying to click an element but its throwing InvalidSelectorException.

These are my dependencies:

<dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-api</artifactId> <version>4.7.1</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-remote-driver</artifactId> <version>4.7.1</version> </dependency> <dependency> <groupId>io.appium</groupId> <artifactId>java-client</artifactId> <version>8.3.0</version> </dependency> <dependency> <groupId>com.github.javafaker</groupId> <artifactId>javafaker</artifactId> <version>1.0.2</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.7.1</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.6.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.2.2</version> </dependency> <dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>5.1.1</version> </dependency> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.10.1</version> <type>maven-plugin</type> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>com.aventstack</groupId> <artifactId>extentreports</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>io.qameta.allure</groupId> <artifactId>allure-testng</artifactId> <version>2.18.1</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.26</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20220320</version> </dependency> <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.2</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.9.1</version> <scope>runtime</scope> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-java</artifactId> <version>7.8.1</version> </dependency> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-testng</artifactId> <version>7.8.1</version> </dependency> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-jvm-deps</artifactId> <version>1.0.6</version> <scope>provided</scope> </dependency> </dependencies> 

**This my runner class: **

package mobilerunner; import java.net.MalformedURLException; import org.apache.log4j.PropertyConfigurator; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import commonuseractions.CommonActionMethods; import io.appium.java_client.android.AndroidDriver; import io.appium.java_client.android.options.UiAutomator2Options; import io.appium.java_client.service.local.AppiumDriverLocalService; import mobile_pageobjects.SauceLab.Login_Page; import utils.DriverFactory; public class SauceLab_Aroon extends CommonActionMethods{ @BeforeClass public static void setUp() throws MalformedURLException { PropertyConfigurator.configure(configFilename); UiAutomator2Options opt = new UiAutomator2Options() .setApp("D:\\Mobile Automation\\APK\\Android.SauceLabs.Mobile.Sample.app.2.7.1.apk") .setAppActivity("com.swaglabsmobileapp.MainActivity") .setAppPackage("com.swaglabsmobileapp") .setAutomationName("UiAutomator2") .setDeviceName("Pixel 2 XL API 31") .eventTimings(); appDriver = new AndroidDriver(new java.net.URL("http://127.0.0.1:4723/wd/hub/"),opt); DriverFactory.setDriver(appDriver); } @Test public void test1() throws Exception { new Login_Page().logIn(); } @AfterClass public void tearDown() { } } 

**This my PageObject class: **

package mobile_pageobjects.SauceLab; import java.time.Duration; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.CacheLookup; import org.openqa.selenium.support.PageFactory; import commonuseractions.CommonActionMethods; import io.appium.java_client.pagefactory.AndroidFindBy; import io.appium.java_client.pagefactory.AppiumFieldDecorator; public class Login_Page extends CommonActionMethods{ @AndroidFindBy(accessibility = "test-Username") @CacheLookup WebElement userName; public Login_Page() { // PageFactory.initElements(new AppiumFieldDecorator(appDriver,Duration.ofSeconds(15)), this); PageFactory.initElements(appDriver, this); } public void logIn() throws Exception { Thread.sleep(15000); clickMethod(userName," UserName input field "); } } 

Here is the Exception printed in console.

org.openqa.selenium.InvalidSelectorException: Unsupported CSS selector '[name='userName']'. Reason: 'Error: 'name' is not a valid attribute. Supported attributes are 'checkable, checked, clickable, enabled, focusable, focused, long-clickable, scrollable, selected, index, instance, description, resource-id, text, class-name, package-name'' For documentation on this error, please visit: https://selenium.dev/exceptions/#invalid_selector_exception Build info: version: '4.7.1', revision: 'c6795baf1a3' System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '17.0.3' Driver info: io.appium.java_client.android.AndroidDriver Command: [d0d24b81-2280-4c6d-9692-afc0af6b9bf9, findElement {using=css selector, value=[name='userName']}] Capabilities {appium:app: D:\Mobile Automation\APK\An..., appium:appActivity: com.swaglabsmobileapp.MainA..., appium:appPackage: com.swaglabsmobileapp, appium:automationName: UiAutomator2, appium:databaseEnabled: false, appium:desired: {app: D:\Mobile Automation\APK\An..., appActivity: com.swaglabsmobileapp.MainA..., appPackage: com.swaglabsmobileapp, automationName: UiAutomator2, deviceName: Pixel 2 XL API 31, eventTimings: true, platformName: android}, appium:deviceApiLevel: 31, appium:deviceManufacturer: Google, appium:deviceModel: sdk_gphone64_x86_64, appium:deviceName: emulator-5554, appium:deviceScreenDensity: 560, appium:deviceScreenSize: 1440x2880, appium:deviceUDID: emulator-5554, appium:eventTimings: true, appium:javascriptEnabled: true, appium:locationContextEnabled: false, appium:networkConnectionEnabled: true, appium:pixelRatio: 3.5, appium:platformVersion: 12, appium:statBarHeight: 84, appium:takesScreenshot: true, appium:viewportRect: {height: 2628, left: 0, top: 84, width: 1440}, appium:warnings: {}, appium:webStorageEnabled: false, platformName: ANDROID} Session ID: d0d24b81-2280-4c6d-9692-afc0af6b9bf9 at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:200) at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:133) at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:53) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:184) at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:262) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:551) at org.openqa.selenium.remote.ElementLocation$ElementFinder$2.findElement(ElementLocation.java:162) at org.openqa.selenium.remote.ElementLocation.findElement(ElementLocation.java:60) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:367) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:359) at org.openqa.selenium.By$PreW3CLocator.findElement(By.java:429) at org.openqa.selenium.By$ByName.findElement(By.java:238) at org.openqa.selenium.support.ByIdOrName.findElement(ByIdOrName.java:50) at org.openqa.selenium.remote.ElementLocation$ElementFinder$1.findElement(ElementLocation.java:136) at org.openqa.selenium.remote.ElementLocation.findElement(ElementLocation.java:80) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:367) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:359) at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:70) at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:39) at jdk.proxy2/jdk.proxy2.$Proxy20.click(Unknown Source) at commonuseractions.CommonActionMethods.clickMethod(CommonActionMethods.java:145) at mobile_pageobjects.SauceLab.Login_Page.logIn(Login_Page.java:27) at mobilerunner.SauceLab_Aroon.test1(SauceLab_Aroon.java:38) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568)

3
  • Am i missing something?
    – Aroon
    CommentedDec 14, 2022 at 6:06
  • It works now after changing the selector like this ---- @CacheLookup @FindBy(xpath = "//android.widget.EditText[@content-desc=\"test-Username\"]") WebElement userName;
    – Aroon
    CommentedDec 14, 2022 at 7:06
  • Check the locator for 'userName', may be you mentioned the wrong type of locator, ex. - instead of XPath, you would've mentioned as CssSelector, check that.
    – AbiSaran
    CommentedDec 14, 2022 at 7:08

2 Answers 2

0

After making some changes to the selector it worked.

here are the changes

@CacheLookup @FindBy(xpath = "//android.widget.EditText[@content-desc=\"test-Username\"]") WebElement userName; 
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – CommunityBot
    CommentedDec 16, 2022 at 17:33
0

You can find your answer here https://discuss.appium.io/t/androidfindby-unsupported-css-selector/36733/3. Using initElements(new AppiumFieldDecorator(appDriver), this); instead of PageFactory.initElements(appDriver, this); should fix your Problem. Please don't use XPath instead of id as it is much slower and not reliable.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.