繁体   English   中英

如何使用Selenium Webdriver和Java提取元素的显示属性

[英]How to extract the display attribute of an element using Selenium Webdriver and Java

无法在div中找到隐藏的元素

<div id="divDuplicateBarcodeCheck" class="spreadsheetEditGui" style="z- 

 index: 1200; width: 640px; height: 420px; top: 496.5px; left: 640px; 

display:block"> ==$0

我想找到显示元素,但是该元素是隐藏的,我也为此编写了代码。

String abc=d.findElement(By.xpath("//div[@id='divDuplicateBarcodeCheck']/"))
.getAttribute("display");
 System.out.println(abc);
 Thread.sleep(3000);
 if(abc.equalsIgnoreCase("block"))      
 {          
 d.findElement(By.id("duplicateBarcodeCheck")).click();   
System.out.println("duplicate barcode Close");                                              
}
else    
{    System.out.println("Barcode selected");}

没有诸如display这样的属性。 它是style属性的一部分。

您可以找到元素并获取其属性样式:

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");}
}

或者,您可以直接使用cssSelector找到该元素(也可以使用xpath找到):

WebElement abc = d.findElement(By.cssSelector("div[id='divDuplicateBarcodeCheck'][style*='display: block']"))

请注意,如果找不到该元素,则以上内容将引发NoSuchElementException 您可以使用try-catch块执行类似的操作,就像在if-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");
}

如果您正确无误,则尝试存档检查是否显示元素。 您可以使用纯硒和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测试!)不应遵循display:nonedisplay:block类的实现细节。 想象一下实现将被更改为通过javascript或其他方式删除元素。 良好的硒测试应始终尝试尽可能代表真实的用户观点。 表示如果UI仍然具有相同的行为,则您的测试仍应成功。 因此,您应该进行更一般的检查-是否显示元素。

这是Seleniums WebElement接口的基本功能之一,或更确切地说,它是isDisplayed()方法。

引用Selenium Java Docs

boolean isDisplayed()

是否显示此元素? 此方法避免了必须解析元素的“样式”属性的问题。 返回:是否显示元素

此外,我建议为此类事情编写一些小的辅助方法,以我的经验,这是您会经常遇到的常见用例。 例如,辅助方法可能看起来像这样:

boolean isElementVisible(final By by) {
    return driver.findElement(by).isDisplayed();
}

boolean isElementVisible(final WebElement element) {
    return element.isDisplayed();
}

如果您使用诸如FluentLeniumSelenide之类的Selenium抽象,则它们将变得更加方便,因为它们为诸如assertJ,hamcrest,junit之类的知名断言库提供断言扩展和自定义匹配器之类的东西。

例如,使用FluentLenium和AssertJ(我个人可以推荐一个堆栈),您的问题的答案就这么简单:

// check if element is displayed
assertThat(el("#divDuplicateBarcodeCheck")).isDisplayed();

// check if element is not displayed
assertThat(el("#divDuplicateBarcodeCheck")).isNotDisplayed();

还有一些想法:

如果可能,还应该使用CSS选择器代替xPath选择器。 CSS选择器不那么脆弱 ,它将加快您的测试速度提高可读性

您应该看看隐式等待,而不是使用线程睡眠(不好的做法)。 您可以自己再次实现这样的辅助方法,例如:

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));
}

或(我会建议)为此使用一个库,例如Awaitility

如果您正在寻找更扩展的示例,可以在这里查看:

似乎需要删除的末尾有一个多余的/ 此外,您需要为WebvisibilityOfElementLocated() OfElementLocated visibilityOfElementLocated()引入WebDriverWait 因此,您的代码行实际上将是:

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");
}

实际上, 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");
}

Q1。 我想找到显示元素,但是该元素是隐藏的,我也为此编写了代码。

A1。 根据您的以下代码:

 <div id="divDuplicateBarcodeCheck" class="spreadsheetEditGui" style="z- index: 1200; width: 640px; height: 420px; top: 496.5px; left: 640px; display:block"> ==$0 

它看起来并不隐藏,问题在于您使用了不正确的xpath和element getter。

采用:

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM