2016-12-23 203 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

相關問題