Java Selenium确定选择选项是否可见

Java Selenium确定选择选项是否可见,java,selenium,html-select,Java,Selenium,Html Select,我正试图在一个select元素中选择一个选项,这取决于它是否可见 是否仍然可以确定选择中的选项是否可见?它们在CSS中具有该属性,但selenium没有用于Select元素的.isDisplayed方法 // Select page role permission Select pageRole = new Select (driver.findElement(By.id("page_roles"))); pageRole.selectByIndex(1); 您可以尝试

我正试图在一个select元素中选择一个选项,这取决于它是否可见

是否仍然可以确定选择中的选项是否可见?它们在CSS中具有该属性,但selenium没有用于
Select
元素的
.isDisplayed
方法

    // Select page role permission
    Select pageRole = new Select (driver.findElement(By.id("page_roles")));
    pageRole.selectByIndex(1);

您可以尝试创建包含所选属性的选项列表:

 List<WebElement> visibleOptions = pageRole.findElements(By.cssSelector("Visible css class"));
List visibleOptions=pageRole.findElements(By.cssSelector(“可见css类”);

您可以使用xpath进行此操作

这应该起作用:

var select = driver.findElement(By.xpath("//*[@id='page_roles' and not(contains(@style,'display:none'))]");
//check if select is not null here
Select pageRole = new Select (select);
pageRole.selectByIndex(1);

我们可以直接使用option locator进行检查,如下图所示检查可见性

driver.findElements(By.cssSelector("#page_roles option[value='1']"))
但isDisplayed()如果不存在,将抛出异常。我们必须通过检查findelements中的大小来检查元素的存在

所以你可以做这样的条件

按选项选择:

if(driver.findElements(By.cssSelector("#page_roles option[value='1']")).size() > 0){
    Select pageRole = new Select (driver.findElement(By.id("page_roles")));
    pageRole.selectByIndex(1);
}
   String userRole="Admin";
    if(driver.findElements(By.xpath("//[@id='page_roles'/option[text() ='"+userRole+"']")).size() > 0){
        Select pageRole = new Select (driver.findElement(By.id("page_roles")));
        pageRole.selectByVisibleText(userRole);
    }
按可见文本选择:

if(driver.findElements(By.cssSelector("#page_roles option[value='1']")).size() > 0){
    Select pageRole = new Select (driver.findElement(By.id("page_roles")));
    pageRole.selectByIndex(1);
}
   String userRole="Admin";
    if(driver.findElements(By.xpath("//[@id='page_roles'/option[text() ='"+userRole+"']")).size() > 0){
        Select pageRole = new Select (driver.findElement(By.id("page_roles")));
        pageRole.selectByVisibleText(userRole);
    }

这取决于你所说的可见是什么意思。如果您的意思是
.isDisplayed()
那么所有
选项
都是可见的(根据Selenium),无论它们是否实际可见

例如,转到并查看该页面上有一个
SELECT
元素,该元素包含两个
OPTION
s,“OPTION 1”和“OPTION 2”。如果您使用Selenium找到它们并返回
.isDisplayed()
它们都将返回
true
,即使在默认状态下它们都未被选中(并且用户不可见)

我想你真正想要的是它们是否存在于选项列表中。如果这是你的意思,我们可以确定。下面的函数是针对上面的链接构建的。它在下拉列表中获取选项列表,从每个选项中提取文本,然后比较您传入的字符串以查看它是否是可用选项

public static boolean optionExists(String option)
{
    return new Select(driver.findElement(By.id("dropdown"))).getOptions().stream().map(e -> e.getText()).collect(Collectors.toList())
            .contains(option);
}
你可以这样使用它

String option = "Option 3";
if (optionExists(option))
{
    new Select(driver.findElement(By.id("dropdown"))).selectByVisibleText(option);
}

您可以根据需要将这些链接在一起。您还可以在基于提供的字符串列表设置值的方法中使用optionExists中的代码。它可以在列表中循环,检查每个列表是否存在,然后选择找到的第一个。。。或者类似的。我将把它留给您……

这将不起作用,因为选择和选项通常不包含
display:none
样式。此外,如果问题不存在,它将抛出。使用相关HTML和错误堆栈跟踪更新问题