首页 > 解决方案 > org.openqa.selenium.ElementNotInteractableException,同时单击属性“unselectable = on”的span元素

问题描述

我正在尝试什么:

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement element = driver.findElement(By.xpath("//span[text()='TrailVersion, Testing_Demo']"));

选项1:

element.click();

选项 2:

Actions action = new Actions(driver);
action.click(element).build().perform();
action.moveToElement(element).click().build().perform(); 

  {Giving exception "org.openqa.selenium.JavascriptException: javascript error: Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite." }
       

选项 3:

JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

我想强调一下 span 标签包含<span unselectable = "on"> 我已经尝试了以上所有 3 个选项的属性,但不幸的是,没有任何效果。我也为相同的元素尝试了不同的 Xpath,但徒劳无功。该元素没有唯一的 ID 或类。

谁能帮我解决这个问题?

标签: javaseleniumselenium-webdriverxpathwebdriverwait

解决方案


不可选择的属性

unSelectable属性设置选择过程是否可以从元素的内容开始。If the unSelectable attribute of an element is set to on, then the element is selectable only if the selection starts outside the contents of the element.

在 Firefox、Google Chrome 和 Safari 中,-moz-user-select-webkit-user-select样式属性用于实现类似的功能。

unSelectable属性和-moz-user-select样式-webkit-user-select属性的区别在于-moz-user-select-webkit-user-select样式属性指定是否可以选择元素,而unSelectable属性只指定选择过程是否可以从元素的内容开始。另一个区别是unSelectable属性不是继承的,-moz-user-select-webkit-user-selectstyle 属性是继承的。这意味着unSelectable必须在所有不可选择元素上设置该属性,无论该unSelectable属性是否设置在不可选择元素的父元素上。


这个用例

相关的 HTML 将有助于构建规范的答案。但是,如果元素是动态元素或网站是基于Kendo UI的,则单击需要诱导WebDriverWait的元素elementToBeClickable(),您可以使用以下任一Locator Strategies

  • 使用WebDriverWaitxpath

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[starts-with(., 'TrailVersion') and contains(., 'Testing_Demo')]"))).click();
    
  • 使用动作xpath

    new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[starts-with(., 'TrailVersion') and contains(., 'Testing_Demo')]")))).click().build().perform();
    
  • 使用JavascriptExecutorxpath

    ((JavascriptExecutor)driver).executeScript("arguments[0].click();", new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[starts-with(., 'TrailVersion') and contains(., 'Testing_Demo')]"))));
    

参考

您可以在以下位置找到相关的详细讨论:


推荐阅读