我正在创建一个简单的硒/ junit测试,该测试旨在获取excel数据并将该数据附加到URL字符串的末尾。我编写了一个for循环,该循环应该遍历位于特定工作表中的所有数据。假定在循环完成后关闭当前浏览器窗口,并在下一次迭代的开始启动另一个浏览器会话。
前8个循环运行良好,但是测试结束并抛出“ NoSuchElementException”,引用了第一个条件“ if”中的元素 声明。假设第一个条件语句中的元素不存在,则测试应仅跳转至下一个条件语句(否则为if)。
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import Utility.ExcelUtils;
public class TestClass extends Work{
WebDriver driver;
ExcelUtils excel;
String[] dataArray;
int dataArraySize;
@Before
public void testSetup() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\jkohout\\Documents\\Drivers\\chromedriver1.exe");
driver = new ChromeDriver();
excel = new ExcelUtils();
excel.setExcelFile("C:\\Users\\jkohout\\Documents\\JEMSAPITEST\\TestCases.xlsx", "Decoded");
dataArray = new String[1039];
dataArraySize = dataArray.length;
}
@Test
public void testDataRun() throws Exception {
for(int i=0; i < 1040; i++ ) {
String testCase1 = ExcelUtils.getCellData(i, 0);
driver.get(attUrlString+testCase1);
Thread.sleep(2000);
if(driver.findElement(By.xpath("//*[@id='javascriptEnabled']/div/div/img")).isDisplayed()) {
System.out.println("404 page not found: Pass!: " + i);
System.out.println(dataArraySize);
} else if (driver.findElement(By.cssSelector("input[id='ct100_masterBody_btnNew']")).isDisplayed()) {
System.out.println("Normal Display: Pass!: " + i);
System.out.println(dataArraySize);
} else {
System.out.println("Fail: " + i);
System.out.println(dataArraySize);
}
}
}
@After
public void closeTest() {
driver.quit();
}
}
这是堆栈跟踪:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='javascriptEnabled']/div/div/img"}
(Session info: chrome=67.0.3396.99)
(Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 10.0.17134 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T15:15:08.936Z'
System info: host: 'JKOHOUT-LT', ip: '10.10.100.126', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_171'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.40.565498 (ea082db3280dd6..., userDataDir: C:\Users\jkohout\AppData\Lo...}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 67.0.3396.99, webStorageEnabled: true}
Session ID: dc93688301cb42d466343c51a9771426
*** Element info: {Using=xpath, value=//*[@id='javascriptEnabled']/div/div/img}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:317)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:419)
at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:309)
at Test.TestClass.testDataRunDecoded(TestClass.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
答案 0 :(得分:1)
替换:
Thread.sleep(2000);
if(driver.findElement(By.xpath("//*[@id='javascriptEnabled']/div/div/img")).isDisplayed()) {
System.out.println("404 page not found: Pass!: " + i);
System.out.println(dataArraySize);
} else if (driver.findElement(By.cssSelector("input[id='ct100_masterBody_btnNew']")).isDisplayed()) {
具有:
Thread.sleep(2000);
List<WebElement> list = driver.findElements(By.xpath("//*[@id='javascriptEnabled']/div/div/img"));
if(!list.isEmpty() && list.get(0).isDisplayed()) {
System.out.println("404 page not found: Pass!: " + i);
System.out.println(dataArraySize);
} else if (driver.findElement(By.cssSelector("input[id='ct100_masterBody_btnNew']")).isDisplayed()) {
这个想法不是找到单个元素,而是找到元素列表。如果找不到任何元素,我们将得到一个空列表,但是在这种情况下,不会引发异常。
答案 1 :(得分:1)
只需替换以下代码行:
if(driver.findElement(By.xpath("//*[@id='javascriptEnabled']/div/div/img")).isDisplayed()) {
System.out.println("404 page not found: Pass!: " + i);
System.out.println(dataArraySize);
}
else if (driver.findElement(By.cssSelector("input[id='ct100_masterBody_btnNew']")).isDisplayed()) {
System.out.println("Normal Display: Pass!: " + i);
System.out.println(dataArraySize);
}
收件人:
int size1 = driver.findElements(By.xpath("//*[@id='javascriptEnabled']/div/div/img")).size();
if(size1>0) {
System.out.println("404 page not found: Pass!: " + i);
System.out.println(dataArraySize);
}
int size2 = driver.findElements(By.cssSelector("input[id='ct100_masterBody_btnNew']")).size();
if(size2>0) {
System.out.println("Normal Display: Pass!: " + i);
System.out.println(dataArraySize);
}
说明:
只需将findElement()
替换为findElements()
。请注意,如果{strong> list 的大小大于 1 ,findElements()
将返回一个列表,然后您可以继续执行代码。我必须说这将是一种解决方法。因为如果没有元素,则 findElements()会一直等到您在隐式等待中指定的时间。
希望这会有所帮助。