How to interact with the elements within #shadow-root (open) while Clearing Browsing Data of Chrome Browser using cssSelector

前端 未结 3 733
独厮守ぢ
独厮守ぢ 2020-11-22 09:13

I had been following the discussion How to automate shadow DOM elements using selenium? to work with #shadow-root (open) elements.

While in the process

3条回答
  •  爱一瞬间的悲伤
    2020-11-22 09:59

    I had to do a similar test which required clearing browsing the chrome history. A minor difference was that I was clearing the data after going to the advanced section of the pop-up. As you are struggling to click only the "Clear data" button, I'm quite sure that you've missed one or two hierarchy elements mistakenly. Or got confused between sibling and parent elements probably. As per seeing your code, I assume that you already know that to access a particular shadow DOM element you need proper sequencing and it has been explained also quite nicely above.
    Coming right at your problem now, here is my code snippet which is working correctly. The code waits until the data is cleaned and then will proceed to your next action-

    public WebElement expandRootElement(WebElement element) {
    WebElement ele = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot",
            element);
    return ele;
    }
    
    public void clearBrowsingHistory() throws Exception {
    
        WebDriverWait wait = new WebDriverWait(driver, 15);
        driver.get("chrome://settings/clearBrowserData");
    // Get shadow root elements
        WebElement shadowRoot1 = expandRootElement(driver.findElement(By.xpath("/html/body/settings-ui")));
    
        WebElement root2 = shadowRoot1.findElement(By.cssSelector("settings-main"));
        WebElement shadowRoot2 = expandRootElement(root2);
    
        WebElement root3 = shadowRoot2.findElement(By.cssSelector("settings-basic-page"));
        WebElement shadowRoot3 = expandRootElement(root3);
    
        WebElement root4 = shadowRoot3
            .findElement(By.cssSelector("#advancedPage > settings-section > settings-privacy-page"));
        WebElement shadowRoot4 = expandRootElement(root4);
    
        WebElement root5 = shadowRoot4.findElement(By.cssSelector("settings-clear-browsing-data-dialog"));
        WebElement shadowRoot5 = expandRootElement(root5);
    
        WebElement root6 = shadowRoot5
            .findElement(By.cssSelector("cr-dialog div[slot ='button-container'] #clearBrowsingDataConfirm"));
    
        root6.click();
        wait.until(ExpectedConditions.invisibilityOf(root6));
    }
    

    It should work properly in your case too if you don't intend to change any of the options selected by default in the pop-up (In that case, you will have to add a few more codes regarding selecting those checkboxes). Please tell me if this solves your issue. Hope this is helpful I've added a snapshot of the the screen here too- image

提交回复
热议问题