尝试获取阴影元素的属性但得到
ElementNotVisibleException Element with CSS input[type='checkbox'] is not present on screen
<checkbox _ngcontent-ebv-c14="" label="User Access" ng-version="8.0.3" class="ng-star-inserted">
#shadow-root(open)
<label>
<input type="checkbox" name="" value="true">
<span class="checkmark"></span>
<!---->
<span class="label">User Access</span>
</label>
</checkbox>
使用Selenium获取元素属性
WebElement parentElement = webdriver.findElement(By.xpath("//checkbox[@label='User Access']"));
webdriver.getShadowElement(parentElement,"input[type='checkbox']").getAttribute("value").contains("true");
我错过了什么吗?
我这样解决我的问题
public class test {
WebDriver driver;
public WebElement maximizerootelement(WebElement retelement) {
WebElement ele = (WebElement) ((JavascriptExecutor) driver)
.executeScript("return arguments[0].shadowRoot",retelement);
return retelement;
}
@Test
public void mytest() {
driver.get("https://yourlin");
WebElement root1 = driver.findElement(By.tagName("//checkbox[@label='User Access']"));
WebElement shadowRoot = maximizerootelement(root1);
String actualHeading = shadowRoot.findElement(By.cssSelector("input[type='checkbox']")).getText();
Assert.assertEquals("true", actualHeading);
}
}
使用 javascript 执行器,您需要扩展元素以对元素执行操作
我能够通过以下步骤解决此问题:
WebElement ele = (IWebElement)executor.ExecuteScript("return 在此处粘贴你的 jspath"); ele.Click();