我正在使用Java和Selenium编写针对Chrome的测试。有时我需要进入chrome://downloads/并单击CLEAR ALL按钮。我可以通过

RemoteWebDriver driver = (RemoteWebDriver) driverChrome;
        driver.executeScript("window.open();");
        Thread.sleep(500);
        tabs = new ArrayList<String>(driverChrome.getWindowHandles());
        driverChrome.switchTo().window(tabs.get(1));
        Thread.sleep(500);
        driverChrome.get("chrome://downloads/");


但是我无法单击该按钮,无论我用什么xpath表示no such element

最佳答案

下面的JavascriptExecutor示例使用硒执行CLEAR ALL按钮的点击:-

JavascriptExecutor executor = (JavascriptExecutor)driver
executor.executeScript("var dm = document.getElementsByTagName('downloads-manager')[0];var toolbar = dm.shadowRoot.getElementById('toolbar');var actions = toolbar.shadowRoot.getElementById('actions');actions.getElementsByClassName('clear-all')[0].click();");


Chrome Version 50.0.2661.102 m中测试

希望它能对您有所帮助.. :)

10-01 13:41