简体   繁体   中英

Shadow DOM - Unable to Click element (Selenium webdriver nodejs)

I am attempting to click an element in a shadow dom. I am using Selenium Webdriver JS. I currently get access the shadow root. I also can find an element. So I thought the last bit clicking it would be straight forward but cant find out how. There is not much support for Shadow Dom with js.

Line await element.click() is the offending code. But cant work out how to click that element.

// This gets the shadow Root
async function getShadowRoot() {
    let shadowHost;
    await (shadowHost = await driver.wait(until.elementLocated(By.css("body > div.project > sn-component-va-web-client"))),5000);
    return driver.executeScript("return arguments[0].shadowRoot", shadowHost);   
};              
       
//This find element inside the shadow Dom.     
async function findShadowDomElement() {
    let shadowRoot;
    let element;
    await (shadowRoot = getShadowRoot());
    await shadowRoot.then(async (result) => {  
    await (element = result.findElement(By.css("div.menu-item.contact-support-clicker")));    
    await element.click()
    });                         
    return element
}                

I think i found a solution. Used nodeJs + selenium. It's work bad with return so i handle like this.

const shadowHost = await driver.findElement(By.css("body > div.project > sn-component-va-web-client"));

const shadowRoot =  await driver.executeScript("return arguments[0].shadowRoot", shadowHost);

const elem = await shadowRoot.findElement(By.css("div.menu-item.contact-support-clicker"));

elem.click();

in my case its works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM