Any easy way to get the By locator from a WebElement?

6,120 views
Skip to first unread message

Troy

unread,
Jan 11, 2012, 7:28:11 PM1/11/12
to Selenium Users
Hi all,

Is there any way to get the By locator from a WebElement object? I'm
trying to use Selenium (2.16.0) to automate tests for a GWT-based web
application. Tests on pages with a lot of asynchronously operating
elements can tend to be rather fragile...

To make them more reliable, we implemented a "waitFor(By locator)"
method that basically retries "driver.findElement(locator)" up until a
configurable timeout, handling the NoSuchElementException in between
tries. That has worked rather well, but it also means we have the
element's locator info in our pages objects AND also in any spot we
need to call waitFor... Kind of a maintenance nightmare.

If the By locator was an accessible property of WebElement, that would
be very handy! The only post I could find that sounded similar was
from back in 2010, no resolution...

Thanks,
-Troy

Luke Inman-Semerau

unread,
Jan 17, 2012, 7:04:31 AM1/17/12
to seleniu...@googlegroups.com
I've heard that you can wrap the webelement object in such a way that it includes the original By locator that was found with it. (which helps for automatic retries of stale elements). I have not done this myself, but sounds reasonable.

-Luke

> --
> You received this message because you are subscribed to the Google Groups "Selenium Users" group.
> To post to this group, send email to seleniu...@googlegroups.com.
> To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.
>

Ross Patterson

unread,
Jan 17, 2012, 9:04:23 AM1/17/12
to seleniu...@googlegroups.com
It sounds like you're re-inventing WebDriver's "implicit wait" facility. You should give it a try. Check out http://seleniumhq.org/docs/04_webdriver_advanced.html for an explanation.

Ross

Krishnan Mahadevan

unread,
Jan 17, 2012, 9:07:40 AM1/17/12
to seleniu...@googlegroups.com
Ross,
Not intending to hijack the thread here, but is there any way using which I can introspect a WebElement and find out what was the locating strategy that was used to identify it in a straight-forward manner?


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

Luke Inman-Semerau

unread,
Jan 17, 2012, 10:54:20 AM1/17/12
to seleniu...@googlegroups.com
Not by default... You have to add that code to store the By with the returned element (I'm not certain the implementation details, but you will have to extend/override some classes, I see it as a feasible effort... and I've heard of it being done). 

But yes to Ross's point, implicit/explicit waits should be used as a first measure. This technique is only useful if you have a very dynamic page that certain elements appear and disappear quite a few times and you don't want to have "find" the element every time (automatic retry). 

-Luke

Troy

unread,
Jan 18, 2012, 1:30:33 PM1/18/12
to Selenium Users
We tried using the implicit wait functionality, but it wasn't working
for us. I don't know if it was the way we set it, or if it's just the
way WebDriver is interacting with a GWT application, or maybe
something in WebDriver itself... We set it through the
driver.manage().timeouts().implicitlyWait() method, but it just didn't
seem to do any polling. :-\

I've pasted in the method we use to fire up a new WebDriver, if anyone
cared to take a look and see if we were setting it incorrectly...


public WebDriver newBrowser(BrowserType browserType)
{
    WebDriver newBrowser;

    // If browserType is "random", randomly pick one of the first 3
browsers Firefox, Chrome, or IE.
    if (browserType.equals(BrowserType.random))
    {
        browserType = BrowserType.values()[new Random().nextInt(3)];
    }

    // Which browser are we testing with?
    switch (browserType)
    {
        case chrome:
            String chromeDriverExePath =
System.getProperty("chrome.driver.exe.location");
            if (chromeDriverExePath == null)
                chromeDriverExePath = "";
            System.setProperty("webdriver.chrome.driver",
chromeDriverExePath + "../extras/chromedriver.exe");
            // Next 2 lines are necessary to run Chrome browser
maximized
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--start-maximized");
            newBrowser = new ChromeDriver(options);
            break;
        case htmlunit:
            newBrowser = new HtmlUnitDriver(true);
            break;
        case ie:
            // To test with IE, enable "Protected Mode" in the
Security tab of the Internet Options
            newBrowser = new InternetExplorerDriver();
            // Workaround to maximize, until WebDriver issue 174 is
resolved.
            newBrowser.findElement(By.xpath("//
html")).sendKeys(Keys.F11);
            break;
        case opera:
            newBrowser = new OperaDriver();
        case firefox:
        default:
            newBrowser = new FirefoxDriver();
            // Workaround to maximize, until WebDriver issue 174 is
resolved.
            newBrowser.findElement(By.xpath("//
html")).sendKeys(Keys.F11);
            break;
    }

    // Set the default implicit timeout, how long it will wait before
    // failing when you look for an element that doesn't exist.
   
newBrowser.manage().timeouts().implicitlyWait(Integer.parseInt(testEnvironment.getProperty("DefaultWaitTime")),
TimeUnit.SECONDS);
    return newBrowser;
}

On Jan 17, 6:04 am, Ross Patterson <rpatter...@parature.com> wrote:
> It sounds like you're re-inventing WebDriver's "implicit wait" facility.  You should give it a try.  Check outhttp://seleniumhq.org/docs/04_webdriver_advanced.htmlfor an explanation.
Reply all
Reply to author
Forward
0 new messages