How to modify CSS properties?

Hi there,
I have next problem with my tests. In website I have class which have CSS properties “display: none”. Because of this they are invisible and I can’t get the text from them. My question is: How to modify this values? I can use WebUI.getCSSValue bo how to change its value!?

Any Idea? :frowning:

Hi Przemek, did you mean that you want to use function getText() to get the text from element with properties “display: none”? or using the verifyElementText keyword to verify the text?

I want to change value from “none” to “block” in css property “display”. How can I go that?

I’m sure it’s possible with new keyword “executeJavascript” but how?!

Use this JS

document.getElementsByClassName("").setAttribute(“style”,“display: block”)

Key word for Java Script executor

@Keyword
public void executeJs(String javaScript){
WebDriver driver = DriverFactory.getWebDriver()
JavascriptExecutor executor = ((JavascriptExecutor)driver)
executor.executeScript(javaScript, null)
}

Nothing works… Anyone can explain me how use “WebUI.executeJavaScript” for change css value of ‘display’ for this field :

css.png

I have checked and the following script is working propertly. You might need to update the ‘id’ of element with the correct one.

WebUI.executeJavaScript(“document.getElementById(‘btn-make-appointment’).setAttribute(‘value’, ‘Child1’)”, null)

1 Like

This is working example

WebUI.openBrowser(‘https://www.w3schools.com/howto/howto_js_toggle_hide_show.asp’)

WebUI.maximizeWindow()

WebUI.waitForPageLoad(30)

WebUI.waitForElementVisible(findTestObject(‘Object Repository/W3School/ToggleButton’), 30)

WebUI.click(findTestObject(‘Object Repository/W3School/ToggleButton’))

println (WebUI.getAttribute(findTestObject(‘Object Repository/W3School/HiddenElement’), “style”))

WebUI.delay(2)

WebElement = WebUiCommonHelper.findWebElement(findTestObject(‘Object Repository/W3School/HiddenElement’), 30)

CustomKeywords.‘com.example.js.test.executeJs’(“arguments[0].setAttribute(“style”,“display: block”)”, WebElement)

println (WebUI.getAttribute(findTestObject(‘Object Repository/W3School/HiddenElement’), “style”))

WebUI.delay(2)

WebUI.closeBrowser()

=================Custom Keyword=============================

public class test {

@Keyword
public void executeJs(String javaScript,WebElement element){
	WebDriver driver = DriverFactory.getWebDriver()
	JavascriptExecutor executor = ((JavascriptExecutor)driver)
	executor.executeScript(javaScript, element)
}

}

I’ve got it! Thanks!