0

I'm using Java, Selenium Webdriver in Eclipse.

I wrote a helper method to wait for an element present, scroll to it, wait for the element to be visible and click it. Here's what I have:

protected void waitScrollWaitClick(By by, String scroll)
{
    wait.until(ExpectedConditions.presenceOfElementLocated(by));
    getJse().executeScript("$('.mCustomScrollbar#" + scroll + "').mCustomScrollbar('scrollTo',document.querySelector(\"" + by.selector + "\"), {scrollInertia:0})");
    wait.until(ExpectedConditions.visibilityOfElementLocated(by));
    getDriver().findElement(by).click();

Now the issue I'm having is in that second line in the method. I am passing a By object. This works for the conventional Webdriver methods on lines 1,3,4. But since we are using a custom scrollbar for our web app, I need to use that JavascriptExecutor class (the getJse()) to scroll on the proper div #id (thus passing in the 'scroll' argument). To use that JSE I just need the CSS selector, not the whole By object. If I add a breakpoint and look, the By object contains a 'selector' field that has what I want (in Eclipse there's a red square icon with an 'F' on it), but I can't seem to access it. I tried with the "by.selector" in the code above, but that is a compile error.

How can use that selector field? I'm not Java expert, so maybe I'm missing something obvious. I guess I don't understand why I can stop on a breakpoint, see the By object I created in the Variables tab, expand the By object and see the 'selector' field I want, but just can't access it.

Atom999
  • 432
  • 5
  • 17

1 Answers1

1

The easy answer is that you cannot get the CSS selector from a By type, or even WebElement type. This is because the WebElements themselves are found by the By class. In case the By specified was a xpath there would be no way to populate the CSS selector.

The long answer specifically for your issue, to get the CSS Selector would be to create it using Javascript. An example would be Florent B.'s answer here. However, I didn't tried myself and I have no idea if it works for all cases.

Now, to address the general issue, instead of using document.querySelector use document.getElementById in case your element has an id.

Or by using document.evaluate to get your element by xpath. You can find an example in the answer posted here.

Community
  • 1
  • 1
Cosmin
  • 2,354
  • 2
  • 22
  • 39
  • Thanks. Florent's answer looks interesting (and complicated), but I'm surprised I cannot access this field of the object I'm already passing in. http://imgur.com/COJh4jC . I rarely have an #ID to use, so getElementById isn't an option. I agree on your point that passing a `By` type using XPath won't work anyways. I am familiar with using `document.evaluate` if I need to for that. Maybe I just need to write a separate helper method for CSS and Xpath and just pass in the string of the locator. Thanks! – Atom999 Sep 21 '16 at 16:11
  • Working in C# on a daily basis, I'm not familiar with how Java works internally, but from the looks of it, it seems that `selector` is something internally used in he By class. You could check more here: https://github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/openqa/selenium/By.java – Cosmin Sep 21 '16 at 16:44
  • Thanks for the help! – Atom999 Sep 21 '16 at 17:58