I have a button onclick event , however, when it is run, in reality, the button is not actually clicked.When I do .isDisplayed(), the element is always found. however, still the test fails. Here i wanted to click on Not now button but in following code didn't work , what should i do ?
WebElement c=driver.findElement(By.xpath("//android.widget.Button[@resource-id='android:id/button2']"));
if(c.isDisplayed())
{
c.click();
}
Thread.sleep(20000);
driver.findElement(By.xpath("//android.widget.EditText[@text='Email']")).sendKeys("testappium@gmail.com");
i also tried following code but i didn't work
driver.findElement(By.xpath("//android.widget.FrameLayout[@bounds='[27,724][1053,1267]']/android.widget.FrameLayout[@bounds='[75,772][1005,1219]']/android.widget.FrameLayout[@resource-id='android:id/content']/android.widget.LinearLayout[@resource-id='android:id/parentPanel']/android.widget.LinearLayout[@resource-id='android:id/buttonPanel']/android.widget.Button[@bounds='[517,1063][777,1207]']")).click();
also tried following code:
driver.findElement(By.xpath("//*[@class='android.widget.Button' and @bounds='[517,1063][777,1207]']")).click();
error which i'm getting is :
Exception in thread "main" org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters.
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
*** Element info: {Using=xpath, value=//android.widget.EditText[@text='Email']}
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.base/java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:239)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:62)
at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.findElement(AndroidDriver.java:1)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
at io.appium.java_client.DefaultGenericMobileDriver.findElementByXPath(DefaultGenericMobileDriver.java:152)
at io.appium.java_client.AppiumDriver.findElementByXPath(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.findElementByXPath(AndroidDriver.java:1)
at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:58)
at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.findElement(AndroidDriver.java:1)
at execution.AppTest.main(AppTest.java:183)
UI Automator Viewer:
You can directly use the id here.
WebElement element=driver.findElement(By.xpath("//android.widget.Button[@text='Not Now']"));
if(element.isDisplayed()){
element.click();
}
Check how many matches are coming for the below XPath
String xPath = "//*[@resource-id='android:id/button2']";
If there are multiple matches then pass the matching index number in the below XPath :
String xPath = "(//*[@resource-id='android:id/button2'])[matching index number]";
Give some delay before searching for an element or performing any operation. Try the below code:
Thread.sleep(3000);
String xPath = "//*[@resource-id='android:id/button2']";
List<WebElement> notNow = driver.findElements(By.xpath(xPath));
if(notNow.size() > 0) {
notNow.get(0).click(); // You can pass the matching index in get() also if there are multiple matches
}
Or you can use the below XPath, give some delay before trying to find :
String xPath = "//android.widget.TextView[@text='Text That You Want to locate']"
I hope it helps...
You can use in this way
WebElement element=driver.findElement(By.xpath("//android.widget.Button[@id='button2']"));
if(element.isDisplayed())
{
element.click();
}
or
string elementId = driver.findElement(By.Id("button2"));
if(elementId.isDisplayed){ element.click(); }
Both should work
using xpath is not recommended in appium.
You can use findElementById like
driver.findElementById("android:id/button2").click()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.