Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 99412

Find element locator on IE webddriver (Any driver actually)

$
0
0

This post is to share a solution I've built to help find elements on webdrivers using System.in, since there are no (at least that I'm aware of) extensions to find locators on IE.

This solution is not ideal and may present issues, however, you can easily implement navigations and gives you a quick response/highlight the elements, for your locator.

In this example, I've implemented XPath, change it as your needs.

Package:

package locator;

Imports:

import org.openqa.selenium.*;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.util.List;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

Class:

public class LocatorHelper { private static WebDriver driver = null;

public static void main(String[] args) {
    createIE();
    driver.get("http://www.google.com.br/");
    JavascriptExecutor js = (JavascriptExecutor) driver;
    Scanner scan = new Scanner(System.in);
    String mainXpath = "";
    String lastXpath = "";

    while (!mainXpath.equals("exit")) {
        System.out.println("Type an xpath: ");
        lastXpath = mainXpath;
        mainXpath = scan.nextLine();
        if (lastXpath.length() > 0) {
            List<WebElement> elements = getElements(lastXpath, true);
            if (elements != null) {
                for (WebElement element : elements) {
                    js.executeScript("arguments[0].style.border='1px solid orange'", element);
                }
            }
        }
        List<WebElement> elements = getElements(mainXpath);
        if (elements != null) {
            for (WebElement element : elements) {
                js.executeScript("arguments[0].style.border='3px solid red'", element);
            }
        }
        System.out.println("---------------------------------------------------------------------------------------------------");
    }
}

private static List<WebElement> getElements(String xpath, boolean... disableLog) {
    List<WebElement> elements = null;
    try {
        elements = driver.findElements(By.xpath(xpath));
        int count = elements.size();
        if (disableLog.length == 0) {
            if (count > 1) {
                System.out.println("The xpath '" + xpath + "' returned " + count + " elements");
            } else if (count == 1) {
                System.out.println("The xpath '" + xpath + "' returned one element");
            } else {
                System.out.println("The xpath '" + xpath + "' returned no elements");
            }
        }
    } catch (InvalidSelectorException e) {
        if (disableLog.length == 0) {
            System.out.println("The xpath '" + xpath + "' is not valid");
        }
    }
    return elements;
}

private static void createIE() {
    //your driver locator
    System.setProperty("webdriver.ie.driver", "src/main/resources/drivers/IEDriverServerNew.exe");
    DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
    caps.setCapability("ignoreZoomSetting", true);
    caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
    caps.setCapability(InternetExplorerDriver.ELEMENT_SCROLL_BEHAVIOR, true);
    DesiredCapabilities.internetExplorer().setCapability("ignoreProtectedModeSettings", true);
    caps.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    caps.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);
    caps.setJavascriptEnabled(true);

    driver = new InternetExplorerDriver(caps);
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.manage().window().maximize();
}}

In this image, the locator found a single element Example of a single element located

In this image, the locator found 20 elements Example of 20 elements found

As you type the locators on the terminal, it will highlight the current elements in red and all previous elements found will be highlighted in orange with a thinner line.

Hope it helps


Viewing all articles
Browse latest Browse all 99412

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>