tebeka / selenium

Selenium/Webdriver client for Go
MIT License
2.51k stars 410 forks source link

How can i set style? Such as display #304

Open R1NNA opened 1 year ago

R1NNA commented 1 year ago

When i get text, if this style set display: none. I cant get the text content, and i didn't find how to change the style attribute.

mammuth commented 7 months ago

I was looking for the same. How I solved it, in the end, is by injecting a <style> element into the dome using ExecuteScript:

func injectCSSAsStyleTag(wd WebDriver, cssText string) error {
    js := `
        let styleTag = document.createElement('style');
        document.head.appendChild(styleTag);
        styleTag.type = 'text/css';
        styleTag.appendChild(document.createTextNode('` + cssText + `'));
    `

    _, err := wd.ExecuteScript(js, nil)

    return err
}

I created corresponding CSS roughly like this:

selectorsToHide := []string{
    `[id="whatever"]`,
}
css := strings.Join(selectorsToHide, ", ") + "{display: none;}"
injectCSSAsStyleTag(c.wd, css)

Additionally, I use this for hiding elements where all attributes (class, id, ...) are uglified. To do so, I find the Element via its inner text by using XPath selectors. Then, I extract the uglified id/class and add it to the selectorsToHide