英文:
org.openqa.selenium.ElementNotInteractableException while clicking on span element with attribute "unselectable=on"
问题
我正在尝试的内容:
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();
{出现异常 "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">`
我已经尝试了以上的三个选项,但不幸的是,都没有起作用。我还尝试了不同的 Xpath 来定位相同的元素,但都没有成功。该元素没有唯一的 ID 或类名。
有谁可以帮助我解决这个问题吗?
英文:
What I am trying:
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement element = driver.findElement(By.xpath("//span[text()='TrailVersion, Testing_Demo']"));
Option 1:
element.click();
Option 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." }
Option 3:
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
I would like to highlight that span tag contains attribute <span unselectable = "on">
I have tried all the above 3 options but unfortunately, nothing is working. I have tried different Xpath's for the same element too but in vain. The element has no unique ID or class.
Can anyone please help me resolve the issue?
答案1
得分: 0
unSelectable 属性
unSelectable 属性用于设置是否可以在元素内容中启动选择过程。如果元素的 unSelectable 属性设置为 on
,那么只有在选择从元素内容外部开始时,该元素才可被选择。
>在 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-select
样式属性会继承。这意味着无论非可选择元素的父元素是否设置了 unSelectable
属性,所有不可选择元素都必须设置 unSelectable
属性。
此用例
相关的 HTML 内容将有助于构建规范的回答。然而,如果元素是动态元素或网站基于 Kendo UI,那么要点击该元素,您需要使用 WebDriverWait 等待 elementToBeClickable()
,并且可以使用以下任一 定位策略:
-
使用 WebDriverWait 和
xpath
:new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[starts-with(., 'TrailVersion') and contains(., 'Testing_Demo')]"))).click();
-
使用 Actions 和
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();
-
使用 JavascriptExecutor 和
xpath
:((JavascriptExecutor)driver).executeScript("arguments[0].click();", new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[starts-with(., 'TrailVersion') and contains(., 'Testing_Demo')]"))));
参考
您可以在以下链接中找到相关的详细讨论:
英文:
unSelectable attribute
The unSelectable attribute sets whether the selection process can start in an element's content or not. 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.
>In Firefox, Google Chrome and Safari, the -moz-user-select and -webkit-user-select style properties are used for implementing similar functionality.
The difference between the unSelectable
attribute and the -moz-user-select
and -webkit-user-select
style properties is that the -moz-user-select
and -webkit-user-select
style properties specify whether an element can be selected while the unSelectable
attribute only specifies whether the selection process can start in an element's content or not. Another difference is that the unSelectable
attribute is not inherited while the -moz-user-select
and -webkit-user-select
style properties are inherited. It means that the unSelectable
attribute must be set on all non-selectable elements regardless of whether the unSelectable
attribute is set on the parent element of a non-selectable element or not.
This usecase
The relevant HTML would have been helpful to construct a canonical answer. However if the element is a dynamic element or the website is Kendo UI based, then to click on the element you need to induce WebDriverWait for the elementToBeClickable()
and you can use either of the following Locator Strategies:
-
Using WebDriverWait and
xpath
:new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[starts-with(., 'TrailVersion') and contains(., 'Testing_Demo')]"))).click();
-
Using Actions and
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();
-
Using JavascriptExecutor and
xpath
:((JavascriptExecutor)driver).executeScript("arguments[0].click();", new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[starts-with(., 'TrailVersion') and contains(., 'Testing_Demo')]"))));
Reference
You can find a relevent detailed discussion in:
专注分享java语言的经验与见解,让所有开发者获益!
评论