Selenium WebDriver : How to find the highlighed text on webpage.





/*
* How to find the highlighed text on webpage.
* Programming language : JAVA
*
* We can achieve this with the help og JavaScript indow.getSelection().toString() function
*
*/
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;



public class HighlightedTextOnWebPage {

public static void main(String[] args) throws InterruptedException {

WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = null;

driver.get("https://google.com");
Thread.sleep(5000);
// Try to highlight any text on webpage using the mouse.


if (driver instanceof JavascriptExecutor) {
js = (JavascriptExecutor)driver;
}

String hldText = (String)js.executeScript("return window.getSelection().toString();");
System.out.println("Highlighted Text on WebPage is : " + hldText.trim());

}
}

No comments:

Post a Comment

Android : How to connect your Android device over Wifi using ADB command for App debugging

How to connect your Android device over Wi-Fi using ADB command Sometimes it requires to connect your Android dev...