问题
I have a text area in web page which has no scrollbar initially. But when i exceeds the character by 1000, scrollbar is displayed in the textarea.
I have to verify the same whether scrollbar is present or not in the textarea when i click or edit the same textarea.
回答1:
The client width or height will decrease if a scrollbar is added. So one way would be to check the size before and after.
Here's an example (Java) :
Long height_before = Long.parseLong(element.getAttribute("clientHeight"));
element.sendKeys("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww")
Long height_after = Long.parseLong(element.getAttribute("clientHeight"));
Assert.True((height_before - height_after) > 10);
Or:
Long clientHeight = Long.parseLong(element.getAttribute("clientHeight"));
Long offsetHeight = Long.parseLong(element.getAttribute("offsetHeight"));
Assert.True((offsetHeight - clientHeight) > 10);
回答2:
There are ways you can do it.
1: As mentioned that scroll bar only appears if textarea characters exceed 1000, so you can check the length of text in textarea to find whether scroll bar exists or not.
2: Get the css value for textarea and check if overflow-y
is scroll
. Example for python selenium
element.value_of_css_property('overflow-y')
回答3:
I had done similar verification with following way :-
String originalScrollHt = element.getAttribute("scrollHeight");
element.sendKeys(" 1000 words length string");
String exceededScrollHt = element.getAttribute("scrollHeight");
assertThat(originalScrollHt).isNotEqualToIgnoringCase(exceededScrollHt);
By taking scrollHeight as a element attribute, you will compare and verify scrollbar presence for a given element.
来源:https://stackoverflow.com/questions/40663005/how-to-verify-presence-of-scrollbar-in-textarea-using-selenium