0

我有两种形式:

<form action="" method="post" name="form1" id="form1" target="_self">
///
</form>

<style>
form#form2 {
    display: none;
}
</style>
<form action="" method="get" name="form2" id="form2" target="_blank">
////
</form>

我有以下代码:

WebDriver driver = new HtmlUnitDriver();
List<WebElement> forms = driver.findElements(By.cssSelector("form"));
for (WebElement form : forms) { 
   if (!form.isDisplayed()) break;
   System.out.print(form.ToString() + "\n");
}

在控制台中,我看到:

<form action="" method="post" name="form1" id="form1" target="_self">
<form action="" method="get" name="form2" id="form2" target="_blank">

如何检查表单是否不可见?

问题的解决方案。

WebDriver driver = new HtmlUnitDriver(**true**);
4

1 回答 1

1

您是否有从开发人员工具中获得的真实呈现的 html,如果有,请您粘贴一下吗?

试试这个,代码没有测试,想法是如果 selenium 认为所有表单都显示了,那么我们应该尝试获取表单的 css 值或 style 属性的值,看看它们是如何实际呈现的。(我怀疑你所做的硒部分是正确的,但 html 样式是错误的。)

// following your logic
WebDriver driver = new HtmlUnitDriver();
List<WebElement> forms = driver.findElements(By.cssSelector("form"));
for (WebElement form : forms) { 
   if (form.getCssValue("display") == "none") break;
   // or if (form.getAttribute("style").contains("display: none")) break;
   System.out.print(form.ToString() + "\n");
}

// a version showing more info
WebDriver driver = new HtmlUnitDriver();
List<WebElement> forms = driver.findElements(By.cssSelector("form"));
for (WebElement form : forms) {
   System.out.print(form.ToString() + "\n");
   System.out.print(form.isDisplayed() + "\n"); // how selenium determines
   System.out.print(form.getCssValue("display") + "\n"); // get css display value
   System.out.print(form.getAttribute("style") + "\n"); // get all style values if exists
   System.out.print("==================\n");
}
于 2013-04-19T22:44:38.347 回答