1

I am automating an app using a blend of Coded UI and Selenium in Visual Studio. I have an app that uses a date picker (seems like it's using Bootstrap but not entirely sure). Using XPath, I can locate future dates but for some reason, cannot locate dates in the past (from current date). Anyone have any ideas?

One of the XPaths I used:

//div[@class='md-virtual-repeat-scroller']//div[@class = 'md-virtual-repeat-offsetter']//table[@class = 'md-calendar']//tbody[@class = 'md-calendar-month']//tr[@aria-label = 'Week 4']//td[@id = 'md-11-2017-5-22']

I have tried shorter versions as well to no avail.

enter image description here

  • Are the tables in the future dates differently formatted? What do you get is you use just the ID: "//td[@id = 'md-11-2017-5-22']" – K-8 Oct 17 '17 at 22:43
  • Can you choose the dates in a manual mode? There could be a restriction preventing showing the older dates. Another hint (not directly related to your issue) is to use "contains" condition when you're filtering by css classes. since they could be easily mixed. – Alexey R. Oct 18 '17 at 12:46
  • @K-8 They are not formatted differently. I figured out what was going on. The dev console displays current date/month and +- 3 months. So since this is October, it displays July-September and November-January. If the user scrolls upwards on the calendar and past July, then the dev console displays the xml tree for previous months. I believe this is where the problem lies. WebDriver is trying to locate something by xPath that does not "exist" in the xml tree. Not sure how to get around that. – hungryhippos Oct 18 '17 at 13:46
  • @AlexeyR. Yes I can choose the dates manually. I believe the issue is with the xml tree not displaying previous months until scrolled upwards. Think of long scroll websites- where data exists on the entire page but the bottom portions don't display until the user scrolls downwards. – hungryhippos Oct 18 '17 at 13:47
  • Sounds like your best bet is to use a Javascript executor to scroll your list of dates until the desired date is present in the DOM. – VanderLinden Oct 18 '17 at 19:09
1

Since the problem was caused by a limited data set being loaded, with more data loaded when the users scrolls, I will answer with the code I use to scroll with Javascript execution.

The following code is for Selenium C#.NET.

bool Scroll(IWebElement scrollContainer, bool up)
{
    // Initialize the script
    string scrollScript = System.IO.File.ReadAllText("ScrollHelper.js");
    if (up)
    {
        scrollScript += $" return scrollUp(arguments[0])";
    }
    else
    {
        scrollScript += $" return scrollDown(arguments[0])";
    }
    IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;

    // Scroll - if the scrollContainer.scrollTop value isn't changed, return false
    // This means the scroll didn't occur - there may be no more data to load
    return (bool)executor.ExecuteScript(scrollScript, scrollContainer);
}

Here is the ScrollHelper.js Javascript file with the methods to be read in above.

// Scroll so that the top of the container is now where the bottom was
function scrollDown(container) {
    var initHeight = container.scrollHeight;
    var initTop = container.scrollTop;
    container.scrollTop = initHeight;
    var scrolled = initTop != container.scrollTop;
    return scrolled;
}
// Scroll so that the bottom of the container is now where the top was
function scrollUp(container) {
    var initTop = container.scrollTop;
    var initHeight = container.scrollHeight;
    container.scrollTop = initTop - initHeight;
    var scrolled = initTop != container.scrollTop;
    return scrolled;
}

You could also opt to just include the Javascript as a string in the Scroll() method.

You might have to try a few different elements to determine which is scrollable, but my best guess would be this one:

<div class="md-virtual-repeat-scroller">

Locate that and then pass it into the Scroll() method along with the direction you want to scroll.

  • Please note that in the applications I test, the only way to scroll is down, so it might take a few changes to get the scroll up working right, although I did test the Javascript portion in Chrome and it seems to work. – VanderLinden Oct 19 '17 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.