selenium - Selenium 中 ExpectedConditions.elementToBeSelected 和 elementSelectionStateToBe 之间的区别

标签 selenium selenium-webdriver

selenium 中的 ExpectedConditions.elementToBeSelected 和 elementSelectionStateToBe 有什么区别?如何使用它?能举个例子吗?

最佳答案

要选择的元素

public static ExpectedCondition<java.lang.Boolean> elementToBeSelected(WebElement element)

ElementSelectionStateToBe

public static ExpectedCondition<java.lang.Boolean> elementSelectionStateToBe(WebElement element, boolean selected)

正如您从方法签名中看到的,elementSelectionStateToBe 接收 boolean 作为参数。您可以通过传递参数来检查元素是否被选中,而在 elementToBeSelected 中则需要捕获异常来检查元素是否未被选中。

检查元素是否被选中

// waits for the element to be selected
wait.until(ExpectedCondition.elementSelectionStateToBe(element, true)); 

// waits for the element to be selected
wait.until(ExpectedCondition.elementToBeSelected(element));

检查元素是否未被选择

// waits for the element **not** to be selected
wait.until(ExpectedCondition.elementSelectionStateToBe(element, false));

try {
    // waits for the element to be selected
    wait.until(ExpectedCondition.elementToBeSelected(element));
}
catch (TimeOutException)
{
    // the element is not selected
}

关于selenium - Selenium 中 ExpectedConditions.elementToBeSelected 和 elementSelectionStateToBe 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35208638/

相关文章:

Python- Selenium : How to click on the element with text as "Year" within the webpage

python - 在 Selenium 中从父元素中提取特定的子元素

java - WebDriver 废话

python - 错误 "name ' 由'未定义'使用 Python Selenium WebDriver

python - Django:Selenium-浏览器上的陈旧元素引用

c# - IgnoreExceptionTypes 不起作用(C# Webdriver)

ruby - 如何访问剪贴板数据以实现自动化?

selenium - 获取 DIV 的值(value) - WebDriver (Selenium)

selenium-webdriver - 试图从 Selenium docker 内部访问 localhost

python - Scrapy with Selenium 爬行但不抓取