I am trying to get the color property of the button element using Javascript inside Selenium, before and after clicking the button.
HTML code:
<html>
<head>
<meta charset="utf-8">
<style>
#container
{
width: 100px;
height: 100px;
border: 1px solid #ccc;
overflow-x: scroll;
}
#content
{
margin:4px, 4px;
padding:4px;
width: 500px;
height:500px;
text-align:justify;
background-color: #ccc;
}
button
{
color: orange;
}
</style>
</head>
<body>
<div id="container">
<div id="content">Click the button to slide right!</div>
</div>
<button id="slide" type="button" onclick="dem_func();">Slide right</button>
<script>
function dem_func()
{
document.getElementById('container').scrollLeft += 50;
<!-- Changing the text color of button element-->
document.getElementById('slide').style.color='red';
return document.getElementById('slide').style.color;
}
</script>
</body>
</html>
Working of the HTML code:
Orange color is applied to the button text through CSS. On clicking the button, the Javascript dem_func()
gets invoked which changes the text color of button to red and it should return the text color of the button element.
Selenium code:
//Before clicking the button
String color7 = (String)js.executeScript("return document.getElementById('slide').style.color");
System.out.println("Color of the text of button element: " + color7);
//After clicking the button
String color8 = (String)js.executeScript("return document.getElementById('slide').click();");
System.out.println("Color of the text of button element: " + color8);
String color9 = (String)js.executeScript("return document.getElementById('slide').style.color");
System.out.println("Color of the text of button element: " + color9);
color9
returns red
as expected.
color7
returns null
, but the expected value is orange
color8
returns null
, but the expected value is red
Any reason why both color7
and color8
returns null
?