4 回答

TA貢獻1815條經(jīng)驗 獲得超13個贊
如果我讓你正確,你正在嘗試存檔檢查元素是否顯示。你可以使用普通的硒和java做這樣的事情:
// the @FindBy annotation provides a lazy implementation of `findElement()`
@FindBy(css = "#divDuplicateBarcodeCheck")
private WebElement barcode;
@Test
public void example() {
driver.get("http://some.url");
waitForElement(barcode);
// isDisplay() is natively provided by type WebElement
if (barcode.isDisplayed()) {
// do something
} else {
// do something else
}
}
private void waitForElement(final WebElement element) {
final WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.visibilityOf(element));
}
您的測試(端到端 UI 測試?。┎粦?yīng)堅持使用或 之類的實現(xiàn)細節(jié)。想象一下,實現(xiàn)將被更改以通過javascript或其他方式刪除元素。一個好的硒測試應(yīng)該總是嘗試盡可能好地代表一個真實的用戶觀點。意味著如果 UI 的行為仍然相同,則測試仍應(yīng)成功。因此,您應(yīng)該進行更一般的檢查 - 是否顯示元素。display:nonedisplay:block
這是Seleniums WebElement接口的基本功能之一,或者更確切地說是它的方法。isDisplayed()
引用自Selenium Java Docs:
布爾值 isDisplayed()
是否顯示此元素?此方法避免了必須分析元素的“style”屬性的問題。返回:是否顯示元素
此外,我建議為這樣的事情編寫一些小的幫助器方法,根據(jù)我的經(jīng)驗,這是一個常見的用例,你會更頻繁地面對。例如,幫助器方法可能如下所示:
boolean isElementVisible(final By by) {
return driver.findElement(by).isDisplayed();
}
boolean isElementVisible(final WebElement element) {
return element.isDisplayed();
}
如果你正在使用一些硒抽象,如FluentLenium或Selenide,事情將變得更加方便,因為它們?yōu)楸娝苤臄嘌詭欤ㄈ鏰ssertJ,hamcrest,junit)提供了斷言擴展和自定義匹配器。
例如,使用FluentLenium和AssertJ(我個人可以推薦的堆棧),您的問題的答案看起來就像這樣簡單:
// check if element is displayed
assertThat(el("#divDuplicateBarcodeCheck")).isDisplayed();
// check if element is not displayed
assertThat(el("#divDuplicateBarcodeCheck")).isNotDisplayed();
更多想法:
如果可能,還應(yīng)使用 CSS 選擇器代替 xPath 選擇器。CSS選擇器不那么脆弱,它將加快測試速度并且可讀性更好。
您應(yīng)該查看隱式等待,而不是使用線程睡眠(不良做法)。您可以再次自己實現(xiàn)這樣的幫助器方法,例如:
void waitForElement(final WebElement element) {
final WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.visibilityOf(element));
}
void waitForElement(final By by) {
final WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(by));
}
void waitForElementIsInvisible(final By by) {
final WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.invisibilityOfElementLocated(by));
}
或者(我建議)為此使用一個庫,例如Waitility

TA貢獻1776條經(jīng)驗 獲得超12個贊
沒有 這樣的屬性。它是屬性的一部分。displaystyle
您可以找到該元素并獲取其屬性樣式:
String style = d.findElement(By.xpath("//div[@id='divDuplicateBarcodeCheck']")).getAttribute("style");
if(style.contains("block")) {
d.findElement(By.id("duplicateBarcodeCheck")).click();
System.out.println("duplicate barcode Close");
} else {
System.out.println("Barcode selected");}
}
或者你可以直接找到這個元素(也可以使用xpath):cssSelector
WebElement abc = d.findElement(By.cssSelector("div[id='divDuplicateBarcodeCheck'][style*='display: block']"))
請注意,如果未找到該元素,則上述內(nèi)容將拋出。您可以使用 block 執(zhí)行類似的操作,就像在語句中所做的那樣,如下所示:NoSuchElementExceptiontry-catchif-else
try {
d.findElement(By.cssSelector("div[id='divDuplicateBarcodeCheck'][style*='display: block']"));
d.findElement(By.id("duplicateBarcodeCheck")).click();
System.out.println("duplicate barcode Close");
} catch (NoSuchElementException e) {
System.out.println("Barcode selected");
}

TA貢獻1951條經(jīng)驗 獲得超3個贊
似乎有一個額外的/在末尾xpath您需要將其刪除。此外,您需要誘導(dǎo) WebDriverWait for .因此,實際上您的代碼行將是:visibilityOfElementLocated()
String abc = new WebDriverWait(d, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//label[contains(.,'Leave Balance')]//following::div[@id='applyleave_leaveBalance']"))).getAttribute("style");
System.out.println(abc);
if(abc.contains("block"))
{
d.findElement(By.id("duplicateBarcodeCheck")).click();
System.out.println("duplicate barcode Close");
}
else
{
System.out.println("Barcode selected");
}
實際上,塊仍然是一個開銷,你可以通過以下方式實現(xiàn)相同的目標:if()
try {
new WebDriverWait(d, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[contains(.,'Leave Balance')]//following::div[@id='applyleave_leaveBalance']"))).click();
System.out.println("duplicate barcode Close");
} catch (NoSuchElementException e) {
System.out.println("Barcode selected");
}

TA貢獻2012條經(jīng)驗 獲得超12個贊
問題 1.我想找到顯示元素,但元素是隱藏的,我也為它編寫了代碼。
解答 1.按照您的以下代碼:
<div id="divDuplicateBarcodeCheck" class="spreadsheetEditGui" style="z-
index: 1200; width: 640px; height: 420px; top: 496.5px; left: 640px;
display:block"> ==$0
它看起來并不隱藏,問題是你使用了不正確的xpath和元素獲取器。
用:
String abc = d.findElement(By.xpath("//div[@id='divDuplicateBarcodeCheck']"))
.getCssValue("display");
而不是:
=> .getAttribute("display");
使用JavascriptExecutor的替代方法:
JavascriptExecutor jse = (JavascriptExecutor) d;
String displayProperty = (String) jse.executeScript("return
document.getElementById('divDuplicateBarcodeCheck').style.display");
System.out.println("Display property is: "+displayProperty);
添加回答
舉報