这是我的代码
public static void handleStaleElement1(String elementName) throws InterruptedException
{
int count = 0;
for(int i=0; i<4;i++)
try {
WebDriverWait wait = new WebDriverWait(TestFunctionsFactory.driver,60);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(elementName)));
WebElement staledElement = driver.findElement(By.xpath(elementName));
((JavascriptExecutor) driver).executeScript("arguments[0].click();",staledElement);
break;
}
catch (Exception e)
{
e.toString();
System.out.println("Trying to recover from a stale element :" + e.getMessage());
count = count + 1;
}
count = count + 4;
}
它在本地环境中工作完美。但是当在VSTS中触发构建时, 我正在获取StaleElementReferenceException,如下所示
2018-07-05T11:41:30.3875369Z FAILED: feature(Test Plan RM-Utilization (Suite ID: 198))
2018-07-05T11:41:30.3875369Z Runs Cucumber Feature
2018-07-05T11:41:30.3875369Z cucumber.runtime.CucumberException: java.lang.AssertionError: Asset SearchProxy element for: DefaultElementLocator 'By.xpath: //div[starts-with(@class,'input-append')]/input' :Expected condition failed: waiting for element to be clickable: Proxy element for: DefaultElementLocator 'By.xpath: //div[starts-with(@class,'input-append')]/input' (tried for 40 second(s) with 500 MILLISECONDS interval)
2018-07-05T11:41:30.3875369Z at cucumber.api.testng.TestNGCucumberRunner.runCucumber(TestNGCucumberRunner.java:69)
2018-07-05T11:41:30.3875369Z at cucumber.api.testng.AbstractTestNGCucumberTests.feature(AbstractTestNGCucumberTests.java:21)
2018-07-05T11:41:30.3875369Z at sun.reflect.GeneratedMethodAccessor34.invoke(Unknown Source)
2018-07-05T11:41:30.3875369Z at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2018-07-05T11:41:30.3875369Z at java.lang.reflect.Method.invoke(Method.java:498)
2018-07-05T11:41:30.3875369Z at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
2018-07-05T11:41:30.3885369Z at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
2018-07-05T11:41:30.3885369Z at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
2018-07-05T11:41:30.3885369Z at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
2018-07-05T11:41:30.3885369Z at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
2018-07-05T11:41:30.3885369Z at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
2018-07-05T11:41:30.3885369Z at org.testng.TestRunner.privateRun(TestRunner.java:744)
2018-07-05T11:41:30.3885369Z at org.testng.TestRunner.run(TestRunner.java:602)
2018-07-05T11:41:30.3885369Z at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
2018-07-05T11:41:30.3885369Z at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
2018-07-05T11:41:30.3885369Z at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
2018-07-05T11:41:30.3885369Z at org.testng.SuiteRunner.run(SuiteRunner.java:289)
2018-07-05T11:41:30.3885369Z at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
2018-07-05T11:41:30.3885369Z at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
2018-07-05T11:41:30.3895369Z at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
2018-07-05T11:41:30.3895369Z at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
2018-07-05T11:41:30.3895369Z at org.testng.TestNG.runSuites(TestNG.java:1144)
2018-07-05T11:41:30.3895369Z at org.testng.TestNG.run(TestNG.java:1115)
2018-07-05T11:41:30.3895369Z at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:283)
2018-07-05T11:41:30.3895369Z at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:75)
2018-07-05T11:41:30.3895369Z at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:120)
2018-07-05T11:41:30.3895369Z at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:386)
2018-07-05T11:41:30.3895369Z at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:323)
2018-07-05T11:41:30.3895369Z at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:143)
请帮助我修复。