猿问

如何使用Selenium Webdriver和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");}


慕码人2483693
浏览 157回答 4
4回答

萧十郎

如果我让你正确,你正在尝试存档检查元素是否显示。你可以使用普通的硒和java做这样的事情:// the @FindBy annotation provides a lazy implementation of `findElement()`&nbsp;@FindBy(css = "#divDuplicateBarcodeCheck")private WebElement barcode;@Testpublic void example() {&nbsp; &nbsp; driver.get("http://some.url");&nbsp; &nbsp; waitForElement(barcode);&nbsp; &nbsp; // isDisplay() is natively provided by type WebElement&nbsp; &nbsp; if (barcode.isDisplayed()) {&nbsp; &nbsp; &nbsp; &nbsp; // do something&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; // do something else&nbsp; &nbsp; }}private void waitForElement(final WebElement element) {&nbsp; &nbsp; final WebDriverWait wait = new WebDriverWait(driver, 5);&nbsp; &nbsp; wait.until(ExpectedConditions.visibilityOf(element));}您的测试(端到端 UI 测试!)不应坚持使用或 之类的实现细节。想象一下,实现将被更改以通过javascript或其他方式删除元素。一个好的硒测试应该总是尝试尽可能好地代表一个真实的用户观点。意味着如果 UI 的行为仍然相同,则测试仍应成功。因此,您应该进行更一般的检查 - 是否显示元素。display:nonedisplay:block这是Seleniums WebElement接口的基本功能之一,或者更确切地说是它的方法。isDisplayed()引用自Selenium Java Docs:布尔值 isDisplayed()是否显示此元素?此方法避免了必须分析元素的“style”属性的问题。返回:是否显示元素此外,我建议为这样的事情编写一些小的帮助器方法,根据我的经验,这是一个常见的用例,你会更频繁地面对。例如,帮助器方法可能如下所示:boolean isElementVisible(final By by) {&nbsp; &nbsp; return driver.findElement(by).isDisplayed();}boolean isElementVisible(final WebElement element) {&nbsp; &nbsp; return element.isDisplayed();}如果你正在使用一些硒抽象,如FluentLenium或Selenide,事情将变得更加方便,因为它们为众所周知的断言库(如assertJ,hamcrest,junit)提供了断言扩展和自定义匹配器。例如,使用FluentLenium和AssertJ(我个人可以推荐的堆栈),您的问题的答案看起来就像这样简单:// check if element is displayedassertThat(el("#divDuplicateBarcodeCheck")).isDisplayed();// check if element is not displayedassertThat(el("#divDuplicateBarcodeCheck")).isNotDisplayed();更多想法:如果可能,还应使用 CSS 选择器代替 xPath 选择器。CSS选择器不那么脆弱,它将加快测试速度并且可读性更好。您应该查看隐式等待,而不是使用线程睡眠(不良做法)。您可以再次自己实现这样的帮助器方法,例如:void waitForElement(final WebElement element) {&nbsp; &nbsp; final WebDriverWait wait = new WebDriverWait(driver, 5);&nbsp; &nbsp; wait.until(ExpectedConditions.visibilityOf(element));}void waitForElement(final By by) {&nbsp; &nbsp; final WebDriverWait wait = new WebDriverWait(driver, 5);&nbsp; &nbsp; wait.until(ExpectedConditions.visibilityOfElementLocated(by));}void waitForElementIsInvisible(final By by) {&nbsp; &nbsp; final WebDriverWait wait = new WebDriverWait(driver, 5);&nbsp; &nbsp; wait.until(ExpectedConditions.invisibilityOfElementLocated(by));}或者(我建议)为此使用一个库,例如Waitility

叮当猫咪

没有 这样的属性。它是属性的一部分。displaystyle您可以找到该元素并获取其属性样式:String style = d.findElement(By.xpath("//div[@id='divDuplicateBarcodeCheck']")).getAttribute("style");if(style.contains("block")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; d.findElement(By.id("duplicateBarcodeCheck")).click();&nbsp; &nbsp;&nbsp; &nbsp; System.out.println("duplicate barcode Close");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;} else {&nbsp; &nbsp;&nbsp; &nbsp; System.out.println("Barcode selected");}}或者你可以直接找到这个元素(也可以使用xpath):cssSelectorWebElement abc = d.findElement(By.cssSelector("div[id='divDuplicateBarcodeCheck'][style*='display: block']"))请注意,如果未找到该元素,则上述内容将抛出。您可以使用 block 执行类似的操作,就像在语句中所做的那样,如下所示:NoSuchElementExceptiontry-catchif-elsetry {&nbsp; &nbsp; d.findElement(By.cssSelector("div[id='divDuplicateBarcodeCheck'][style*='display: block']"));&nbsp; &nbsp; d.findElement(By.id("duplicateBarcodeCheck")).click();&nbsp; &nbsp; System.out.println("duplicate barcode Close");&nbsp;&nbsp;} catch (NoSuchElementException e) {&nbsp; &nbsp; &nbsp;System.out.println("Barcode selected");}

饮歌长啸

似乎有一个额外的/在末尾xpath您需要将其删除。此外,您需要诱导 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")){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; d.findElement(By.id("duplicateBarcodeCheck")).click();&nbsp; &nbsp;&nbsp; &nbsp; System.out.println("duplicate barcode Close");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}else&nbsp; &nbsp;&nbsp;{&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; System.out.println("Barcode selected");}实际上,块仍然是一个开销,你可以通过以下方式实现相同的目标:if()try {&nbsp; &nbsp; new WebDriverWait(d, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[contains(.,'Leave Balance')]//following::div[@id='applyleave_leaveBalance']"))).click();&nbsp; &nbsp; System.out.println("duplicate barcode Close");&nbsp;&nbsp;} catch (NoSuchElementException e) {&nbsp; &nbsp; &nbsp;System.out.println("Barcode selected");}

繁花如伊

问题 1.我想找到显示元素,但元素是隐藏的,我也为它编写了代码。解答 1.按照您的以下代码:<div id="divDuplicateBarcodeCheck" class="spreadsheetEditGui" style="z-&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;index: 1200; width: 640px; height: 420px; top: 496.5px; left: 640px;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; display:block"> ==$0它看起来并不隐藏,问题是你使用了不正确的xpath和元素获取器。用:String abc = d.findElement(By.xpath("//div[@id='divDuplicateBarcodeCheck']")).getCssValue("display");而不是:&nbsp;=> .getAttribute("display");使用JavascriptExecutor的替代方法:JavascriptExecutor jse = (JavascriptExecutor) d;String displayProperty = (String) jse.executeScript("return&nbsp;document.getElementById('divDuplicateBarcodeCheck').style.display");System.out.println("Display property is: "+displayProperty);
随时随地看视频慕课网APP

相关分类

Java
我要回答