2016-12-23 154 views
2

我的测试在我运行测试时掉下来了,我再次运行测试,测试通过,代码不会改变,但结果不同。为什么? 这是我的代码:org.openqa.selenium.ElementNotVisibleException:元素不可见

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

您可能需要使用嵌入式方法要等到目标元素变得可见:

// Set your own value of timeoutInSeconds 
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds); 
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[text()='Product Category']"))); 
+0

现在我的错误:org.openqa.selenium.TimeoutException:预期条件失败:等待位于由By.linkText元件的可见性:iMac电脑(试过5秒与500毫秒间隔) –

+0

这是点击“产品类别”后你的下一步,对吧?尝试'wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“// a [contains(text(),'iMacs')]”)));' – Andersson

+0

我试过了: wait.until(ExpectedConditions.visibilityOfElementLocated 。xpath(“// a [contains(text(),'iMacs')]”))); wait.until(ExpectedConditions.elementToBeClickable(By.xpath(“”))); –

0

你一定要试试Webdriver等待消除此错误。输入你想要触发等待的xpath表达式。

WebDriverWait wait = new WebDriverWait(driver, 30); 
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("<Enter your xpath expression here>"))); 
+0

现在我的错误:org.openqa.selenium.TimeoutException:预期的条件失败:等待元素可点击:By.xpath:// a [text( )='附件'](使用500次MILLISECONDS时间间隔尝试30秒) –

+0

如果您可以提供您想要点击的HTML前端代码和屏幕截图,这将会很有帮助。在您的HTML中,请检查iframe。 – Maninder

相关问题