我有两种形式:
<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**);
答案 0 :(得分: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");
}