Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can i set style? Such as display #304

Open
R1NNA opened this issue Oct 27, 2022 · 1 comment
Open

How can i set style? Such as display #304

R1NNA opened this issue Oct 27, 2022 · 1 comment

Comments

@R1NNA
Copy link

R1NNA commented Oct 27, 2022

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
Copy link

mammuth commented Jan 25, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants