Reputation: 9
I'm trying to capture screenshots using selenium webdriver on a page that has textarea with a large scrollable text. I need to capture screenshots of all the texts in the textarea. With my code, I am able to scroll down on the textarea and take screenshots but need to stop once all the texts are shown. Here is the code
while(!textDisplayedInPage("text at the bottom of textarea element")){
takeScreenShot();
scrollDown();
}
public boolean textDisplayedInPage(String text){
WebElement element = null;
try{
element =getDriver().findElement(By.xpath("//*[contains(text(),'"+text+"')]"));
}catch (Exception e){}
if(element != null){
return true;
};
return false;
}
problem is the textDisplayedInPage() method doesn't work and selenium finds the element on the first screen even though it's not displayed yet and breaks the loop. I have tried few answers that I found of similar issues here but all fails to deliver what's displayed. Any insight on how to capture or verify displayed text of a scrollable element would help.
Upvotes: 0
Views: 67
Reputation: 49
When Selenium looks for an element it checks inside DOM-tree and even though you can't see whole text yet it's in there. If you need to retrieve and verify some text than probably element.getText() would be enough for this, but if you absolutely need to take screenshots thеn you can use JavaScriptExecutor and some scrolling down JS code:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById(id_attribute_value).scrollHeight;", "");
Upvotes: 1