我使用的Selenium WebDriver與一個框架(黃瓜)實現,我面臨一個問題,等待一個元素被加載之前執行一個動作。如何在沒有By定位器的情況下使用WebDriverWait.until?
最初,我想使用隱式等待,但如果一個元素沒有立即加載,它會等待超時。通常情況下,它使我的測試時間比我想要的要長。
然後,我想使用顯式等待來儘可能縮短每個案例的等待時間。 問題是WebDriverWait.until中的ExpectedConditions上的大多數元素都在尋找位於By定位器(它們是ClassName,CssSelector,Id,LinkText,Name,PartialLinkText,Tagname或XPath)的元素。 我在用於點擊webelement的常規函數中使用WebDriverWait.until。 我測試的網站上的webelements是由dojo生成的,沒有靜態ID。它們並不總是具有其他類型的定位器,或者它們不是靜態的。 開發人員然後向webelement添加了一個名爲data-automation-id的附加屬性。我想在明確的等待中使用這個屬性,但是找不到一種方法來做到這一點。
我嘗試使用以下代碼來使用XPath:
public void clickOnDataAutomationId(String dataautomationid) {
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//" + findDataAutomationId(dataautomationid).getAttribute("tagName") + "[contains(@data-automation-id, '" + dataautomationid + "')]")));
findDataAutomationId(dataautomationid).click();
}
findDataAutomationId()是返回包含所述數據的自動化-ID作爲FluentWebElement第一webelement的功能。
問題是如果webelement沒有立即加載,findDataAutomationId會失敗,這會使WebDriverWait.until無意義。您是否看到另一種方法來解決By Locators在不重構網站的情況下?
請發表您的findDataAutomationId片斷 –
這就是: 公共FluentWebElement findDataAutomationId(字符串dataautomationid){ 回報的FindFirst(getDataAutomationId(dataautomationid)); (DataAutomationID = {0}]「,dataautomationid);返回format(」[data-automation-id = {0}]「。 } –