当我运行测试时,我的测试失败了,我再次运行测试,测试通过,代码不会改变,但结果是不同的。为什么? 这是我的代码:
public ProductPage clickOnAccessories(){
//Click on link “Accessories”
waitForElementToBeDisplayed(driver.findElement(By.xpath("//a[text()='Product Category']")), 200);
Actions act = new Actions(driver);
act.moveToElement(driver.findElement(By.xpath("//a[text()='Product Category']")));
act.perform();
link_Accessories.click();
//Expected: Page “Accessories” has been opened with correct url, headline, tab title
Assert.assertTrue(driver.getCurrentUrl().equals("http://store.demoqa.com/products-page/product-category/accessories/"));
Assert.assertTrue(driver.getTitle().contains("Accessories"));
Assert.assertTrue(tab_title_Accessories.getText().contains("customers"));
return PageFactory.initElements(getDriver(), ProductPage.class);
}
public void waitForElementToBeDisplayed(final WebElement element, int defaultWaitTime) {
for (int i = 500; i < defaultWaitTime; i += 100) {
Utils.waitTime(500);
if (isElementDisplayed(element)) { return; }
}
}
protected boolean isElementDisplayed(final WebElement element) {
boolean isDisplayed = false;
try {
final String tagName = element.getTagName();
if (!tagName.isEmpty()) { isDisplayed = element.isDisplayed(); }
return isDisplayed;
} catch (final Exception e) { }
return isDisplayed;
}
这是我的错误:
org.openqa.selenium.ElementNotVisibleException: element not visible
(Session info: chrome=55.0.2883.87)
(Driver info: chromedriver=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed),platform=Windows NT 6.3.9600 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 51 milliseconds
Build info: version: 'unknown', revision: 'c7b525d', time: '2016-09-01 14:57:44 -0700'
System info: host: 'Gaga', ip: '192.168.1.6', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_101'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed), userDataDir=C:\Users\Dragana\AppData\Local\Temp\scoped_dir10100_17794}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=55.0.2883.87, platform=WIN8_1, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: a24c7146c1accfdd3ef521786f4cb399
答案 0 :(得分:0)
您可能需要使用嵌入式方法等待目标元素变为可见:
// Set your own value of timeoutInSeconds
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[text()='Product Category']")));
答案 1 :(得分:0)
您必须尝试Webdriver等待消除此错误。输入要在其中触发等待的xpath表达式。
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("<Enter your xpath expression here>")));