有一种情况,我将鼠标悬停在元素上,然后开始显示一条垂直线,并且我想在悬停后getCSSValue
该元素。
通常情况下,无需直接使用 xpath.getCSSValue("color");
悬停即可为我提供颜色,但我想要的是首先将鼠标悬停在该元素上,然后它将开始显示红色垂直线,并且然后获取颜色值。
我知道/尝试过,在操作类方法上使用 getCSSValue("color")
,但操作类不允许这样做。
public By hoverOnAccount = By.xpath("myxpath");
WebElement abcd = driver.findElement(hoverOnAccount);
String abc = abcd.getCssValue("color");
有人可以建议吗?
最佳答案
这应该有效,请同时添加您尝试过的操作方法。
WebElement hoverElement = driver.findElement(yourElementSelector);
Actions action = new Actions(driver);
action.moveToElement(hoverElement).build().perform();
hoverElement.getCSSValue("color");
如果该操作不起作用,您可以使用 JavaScript Executor
尝试该操作:
String javaScript = "var evObj = document.createEvent('MouseEvents');" +
"evObj.initMouseEvent(\"mouseover\",true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);" +
"arguments[0].dispatchEvent(evObj);";
((JavascriptExecutor)driver).executeScript(javaScript, hoverElement);
关于java - 在 Selenium 中,我可以在鼠标悬停操作后执行 getCSSValue ("color"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59210744/