I need to get a border style of an element. I tried the following code:
sample = page.execute_script("
var x = window.document.querySelector('#{path}');
a = window.getComputedStyle(x, null).getPropertyValue('border');
return a;
")
puts sample
It's printing null.
(Note: Border of the element is - Solid)
Based on your previous questions, I am assuming you are using selenium-webdriver.
The native selenium-webdriver element has methods to check computed styles - Element#css_value.
If you have a page HTML of:
<html>
<body>
<p style="border-style:solid;">A solid border.</p>
</body>
</html>
Then you can get the border style using:
puts page.first('p').native.css_value('border-style')
#=> 'solid'
Notice in the code that:
page.first('p')
native
is used to get the selenium-webdriver elementcss_value
is used to get the computed style. Note that the css_value method has to be passed the paramater 'border-style' not the shorthand 'border'.Update:
For your specific example:
page.find_by_id('option')
Considering the above, you can check the computed border style using:
puts page.find_by_id('option').native.css_value('border-bottom-style')
#=> "solid"
puts page.find_by_id('option').native.css_value('border-left-style')
#=> "solid"
puts page.find_by_id('option').native.css_value('border-right-style')
#=> "solid"
puts page.find_by_id('option').native.css_value('border-top-style')
#=> "solid"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With