我有同樣的問題。但是我在頁面上的元素非常奇怪。其中一些硒可以點擊,雖然他們不可點擊,其中一些硒不能點擊,但可以發送鑰匙給他們。經過幾個小時的思考,我寫了通用方法,檢查元素是否啓用。
與程序員交談之後,我已經知道,他們在這個頁面上使用了一些特殊的Select,它看起來像Div和Input一樣。他說,我可以通過檢查屬性Class來檢查它是否失效。如果有'select2-container-disabled',那麼這個輸入被禁用。
我改變我的方法。現在看起來是這樣的:
public boolean isNotClickable(WebElement... elements) {
List<WebElement> elementsChecked = new ArrayList<>();
List<WebElement> elementsToCheckByClass = new ArrayList<>();
List<WebElement> elementsToCheckByClick = new ArrayList<>();
List<WebElement> elementsToCheckBySendKeys = new ArrayList<>();
for (WebElement checkedElement : elements) {
log.info("Checking, that element [" + getLocator(checkedElement) + "] is not clickable by isEnabled()");
if (checkedElement.isEnabled()) {
elementsToCheckByClass.add(checkedElement);
} else {
elementsChecked.add(checkedElement);
}
}
if (!elementsToCheckByClass.isEmpty()) {
for (WebElement checkedByClassElement : elementsToCheckByClass) {
log.info("Checking, that element [" + getLocator(checkedByClassElement) + "] is not clickable by class");
String classOfElement = checkedByClassElement.getAttribute("class");
List<String> classes = new ArrayList<>(Arrays.asList(classOfElement.split(" ")));
if (!classes.contains("select2-container-disabled")) {
elementsToCheckByClick.add(checkedByClassElement);
} else {
elementsChecked.add(checkedByClassElement);
}
}
}
if (!elementsToCheckByClick.isEmpty()) {
WebDriverWait wait = new WebDriverWait(driverUtils.getDriver(), 1);
for (WebElement checkedByClickElement : elementsToCheckByClick) {
log.info("Checking, that element [" + getLocator(checkedByClickElement) + "] is not clickable by clicking it");
try {
wait.until(elementToBeClickable(checkedByClickElement));
elementsToCheckBySendKeys.add(checkedByClickElement);
} catch (Exception e) {
elementsChecked.add(checkedByClickElement);
}
}
}
if (!elementsToCheckBySendKeys.isEmpty()) {
for (WebElement checkedBySendKeysElement : elementsToCheckBySendKeys) {
log.info("Checking, that element [" + getLocator(checkedBySendKeysElement) + "] is not clickable by sending keys");
try {
checkedBySendKeysElement.sendKeys("checking");
return false;
} catch (Exception e) {
elementsChecked.add(checkedBySendKeysElement);
}
}
}
return elementsChecked.size() == elements.length;
}
請將您的HTML源代碼複製/粘貼爲文本,而不是圖片。 – slim
我已將HTML源文本添加到此問題。 – SKV