尝试使用Chrome浏览器(V51)的JS控制台上的以下代码访问https://shop.polymer-project.org/的Men's Outer wear部分下的“立即购买”按钮:
document.querySelector('shop-app').shadowRoot.querySelector('shop-home').shadowRoot.querySelector('shop-button');
我尝试使用selenium,第1行:WebElement shopBtn = (WebElement)((JavascriptExecutor)driver).executeScript("return document.querySelector('shop-app').shadowRoot.querySelector('shop-home').shadowRoot.querySelector('shop-button')");
在自动化测试中访问相同的元素
第2行:shopBtn.click();
此代码提供错误:
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Cannot read property 'querySelector' of null
(Session info: chrome=51.0.2704.106)
(Driver info: chromedriver=2.20.353124 (035346203162d32c80f1dce587c8154a1efa0c3b),platform=Mac OS X 10.11.3 x86_64).
在这方面的任何帮助都非常感谢。
您没有指定,但看起来您正在使用Java。这里有一些有用的代码。 JSE不是必需的。
WebDriver driver = new FirefoxDriver();
String searchText = "Men's Outerwear";
driver.get("https://shop.polymer-project.org/");
WebDriverWait wait = new WebDriverWait(driver, 5);
List<WebElement> sections = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector("div.item")));
for (WebElement section : sections)
{
if (section.getText().toLowerCase().contains(searchText.toLowerCase()))
{
section.findElement(By.linkText("SHOP NOW")).click();
break;
}
}
您必须逐个遍历阴影根元素才能找到相应的元素。
document.querySelector('nuxeo-app').shadowRoot.querySelector('nuxeo-document-create-button').shadowRoot.querySelector('paper-fab').shadowRoot.querySelector('iron-icon');
我试图执行此单击,我能够使用Firefox和使用xpath定位器
.//shop-button/a[starts-with(@aria-label, 'Men') and contains(@aria-label, 'Outerwear Shop Now')]
我正在使用Selenium 3。
我不确定我是否使用了正确的方法
干杯