简体   繁体   English

org.openqa.selenium.NoSuchElementException即使存在该元素

[英]org.openqa.selenium.NoSuchElementException even when the element is present

I've 4 test cases defined using TestNG within my Appium project and all 4 test cases performs almost same code execution besides the difference that all 4 test cases run on different parts of the app I'm automating. 我在Appium项目中使用TestNG定义了4个测试用例,除了在我要自动化的应用程序的不同部分上运行所有4个测试用例的区别之外,所有4个测试用例都执行几乎相同的代码执行。 The issue I'm facing is within my test case I need to find an element and get its value which works fine in Test Case 1 but fails for Test Case 2, 3 and 4 respectively. 我面临的问题是在我的测试用例中,我需要找到一个元素并获取其值,该值在测试用例1中可以正常工作,但对于测试用例2、3和4分别失败。 However if I comment out Test Case 1, then the code works fine for Test Case 2 but fails for Test Case 3 and 4. If I run code for Test Case 1, 2, 3 or 4 individually, all of them work. 但是,如果我注释掉测试用例1,那么该代码对于测试用例2可以正常工作,但是对于测试用例3和4则失败。如果我分别为测试用例1、2、3或4运行代码,则所有这些都可以工作。 I'm not able to understand why the code works only for the first test case in execution but fails to find the element in the next test case. 我不明白为什么代码仅对执行中的第一个测试用例起作用,而无法在下一个测试用例中找到元素。

Note that the only option for me to find element is using absolute xpath value since the app I'm trying to automate does not have any other attribute defined. 请注意,我要查找元素的唯一选择是使用绝对xpath值,因为我要自动化的应用程序未定义任何其他属性。

Environment java client 4.1.2 Appium server version: v1.7.2 Selenium: v3.4.0 Desktop OS/version used to run Appium if necessary: Windows 10 x64 Mobile platform/version under test: Fire TV Stick running on Android 5.1.1 Real device or emulator/simulator: Real device 环境 Java客户端4.1.2 Appium服务器版本:v1.7.2硒:v3.4.0必要时用于运行Appium的桌面OS /版本:Windows 10 x64受测试的移动平台/版本:在Android 5.1.1真实设备上运行的Fire TV Stick或模拟器/模拟器:真实设备

Details I've provided the code for Test1 below. 详细信息我在下面提供了Test1的代码。 As mentioned earlier, Test2, Test3 and Test4 are same as Test1. 如前所述,Test2,Test3和Test4与Test1相同。 The code below is the one which fails from second Test onwards even though the code works fine when tested individually and the element is definitely present on the screen when the code is executed. 下面的代码是从第二次测试开始失败的代码,即使在单独测试时该代码可以正常工作,并且在执行该代码时该元素肯定出现在屏幕上。 However the script returns no element found. 但是,脚本未返回任何元素。

Failing code strTime1 = driver.findElementByXPath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.widget.FrameLayout[3]/android.widget.FrameLayout[3]/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.TextView").getText(); 错误代码 strTime1 = driver.findElementByXPath(“ / hierarchy / android.widget.FrameLayout / android.widget.LinearLayout / android.widget.FrameLayout / android.widget.FrameLayout / android.widget.FrameLayout / android.widget.FrameLayout / android。 widget.FrameLayout [1] /android.widget.FrameLayout/android.widget.FrameLayout [3] /android.widget.FrameLayout [3] /android.widget.FrameLayout [2] /android.widget.FrameLayout/android.widget。 FrameLayout / android.widget.TextView“)。getText();

Code To Reproduce Issue 重现问题的代码

@test //Test Case for Home tab
public void Test1_Home() throws InterruptedException, ParseException{
Thread.sleep(delay3); //Wait for 10 seconds to load the content
driver.findElementByXPath("//android.widget.FrameLayout[@content-desc="Home, Button"]").click(); //Code to click Home button
Thread.sleep(delay2); //Wait for 10 seconds to load the content
((AndroidDriver) driver).pressKeyCode(23); //Code to click first video thumbnail button
Thread.sleep(delay2); //Wait for 10 seconds to load the content
//Video pause verification code starts
((AndroidDriver) driver).pressKeyCode(23); //Pause the video
Thread.sleep(1000); //Wait for 1 seconds
strTime1 = driver.findElementByXPath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.widget.FrameLayout[3]/android.widget.FrameLayout[3]/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.TextView").getText();
System.out.println(strTime1);
Thread.sleep(delay1); //Wait for 5 seconds
strTime2 = driver.findElementByXPath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.widget.FrameLayout[3]/android.widget.FrameLayout[3]/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.TextView").getText();
System.out.println(strTime2);
time1 = time.parse(strTime1);
time2 = time.parse(strTime2);
System.out.println(time1);
System.out.println(time2);
if(String.valueOf(strTime1).equals(String.valueOf(strTime2)))
{
boolean result = true; //Set result to true
System.out.println("Home: Video playback pause successful");
Assert.assertTrue(result); //Passes the test case
}
else
{
boolean result = false; //Set result to false
System.out.println("Home: Video playback pause failed");
Assert.assertTrue(result); //Fails the test case
}
//Video pause verification code ends

Thread.sleep(delay2); //Allow the video to play for 10 seconds including buffering time
}

Exception stack traces 异常堆栈跟踪

org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 10.13 seconds

Let me know if any further information is required. 让我知道是否需要进一步的信息。 Any kind of help would be appreciated 任何帮助将不胜感激

could you please try to locate the element with relative xpath or accesability id? 您能否尝试找到具有相对xpath或可访问性ID的元素? Also it is always best practice to use latest versions of appium server and java client as they are continuously making changes to the appium. 同样,最好的方法是始终使用最新版本的appium服务器和java客户端,因为它们不断对appium进行更改。 below is the link for latest java client if you are using maven : https://mvnrepository.com/artifact/io.appium/java-client 如果您使用的是maven,则以下是最新的Java客户端的链接: https : //mvnrepository.com/artifact/io.appium/java-client

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.openqa.selenium.NoSuchElementException - org.openqa.selenium.NoSuchElementException org.openqa.selenium.NoSuchElementException即使xpath正确并添加了等待 - org.openqa.selenium.NoSuchElementException even if the xpath is correct and wait was added org.openqa.selenium.NoSuchElementException:无法找到元素错误 - org.openqa.selenium.NoSuchElementException: Unable to locate element error org.openqa.selenium.NoSuchElementException:无法找到元素: - org.openqa.selenium.NoSuchElementException: Unable to locate element: org.openqa.selenium.NoSuchElementException:无法找到元素:-搜索字段 - org.openqa.selenium.NoSuchElementException: Unable to locate element:- Search field 无法找到元素:org.openqa.selenium.NoSuchElementException - Unable to locate element: org.openqa.selenium.NoSuchElementException webdriver org.openqa.selenium.NoSuchElementException:无法找到元素: - webdriver org.openqa.selenium.NoSuchElementException: Unable to locate element: org.openqa.selenium.NoSuchElementException:无法找到元素: - org.openqa.selenium.NoSuchElementException: Unable to locate element: 在线程“主”org.openqa.selenium.NoSuchElementException 中获取异常:没有这样的元素: - Getting exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: 当我单击子窗口中的按钮时,收到“ org.openqa.selenium.NoSuchElementException:没有这样的元素” - “org.openqa.selenium.NoSuchElementException: no such element” received when i click button in child window
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM