WebElement value = (WebElement) Driver.executeScript("return document.evaluate( '//body//div/iframe' ,document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;");
document.evaluate( xpathExpression, contextNode, namespaceResolver, resultType, result );
- xpathExpression: A string containing the XPath expression to be evaluated.
- contextNode: A node in the document against which the xpathExpression should be evaluated, including any and all of its child nodes. The document node is the most commonly used.
- NamespaceResolver: A function that will be passed any namespace prefixes contained within xpathExpression which returns a string representing the namespace URI associated with that prefix.
- resultType: A constant that specifies the desired result type to be returned as a result of the evaluation. The most commonly passed constant is XPathResult.ANY_TYPE which will return the results of the XPath expression as the most natural type.
- result: If an existing XPathResult object is specified, it will be reused to return the results. Specifying null will create a new XPathResult object
Using selenium we can find any element on a page using something similar to
Driver.findElementById("Id of the element");
WebElement searchbox = null; searchbox = (WebElement) ff.executeScript("return document.getElementById('gsc-i-id1');", searchbox);
you will get the element in the searchbox variable.
Finding elements Selenium command
List\<WebElement\> elements = List\<WebElement\> Driver.findElementByTagName("div");
List elements = (List') ff.executeScript("return document.getElementsByTagName('div');", searchbox);
Changing style attribute of the elements
You can change the style property of elements to change the rendered view of the element. For e.g this is how you can create a border around the about me element on toolsQa home page. See the below images of same element before and after creating border
Here is the code.
Driver.executeScript("document.getElementById('text-4').style.borderColor = 'Red'");
Coloring elements can also help us take screenshots with visual markers to identify problematic elements.
Getting Element Attributes
You can get any value of valid attributes of the element. For eg
this code will get the value of class attribute of an element with id = gsc-i-id1.
String className = Driver.executeScript("return document.getElementById('gsc-i-id1').getAttribute('class');"));
Total Frames in Browser
Beware of this one. It only works in IE, can't get it to work for Mozilla.
Adding an Element in DOM
Just for fun if you want to add an element in the DOM. Let's add a button HTML object
Driver.executeScript("var btn=document.createElement('BUTTON');" + "document.body.appendChild(btn);");
Size of Window
Finding the size of the inner browser window. It is the size of the window in which you see the web page.
Driver.executeScript("return window.innerHeight;") Driver.executeScript("return window.innerWidth;")
Navigating to different Page
ff.executeScript("window.location = 'https://yahoo.com'");
Generate Alert Pop Window
Get InnerText of a Webpage
Get Title of a WebPage