/// <summary> /// Use this method to scroll up/down for a give hieght /// </summary> /// <param name="scrollingLength"></param> /// <param name="scrollDown"> -1 scrolling down and 1 for scrolling up</param> protected void ScrollMore( string elementXPath, double scrollingLengthXAxis = 0, double scrollingLengthYAxis = 10, int scrollDown = -1) { JscriptExecutor.ScrollBy( Driver, elementXPath, scrollDown * scrollingLengthXAxis * 2 /*Adding some random offset*/, scrollDown * scrollingLengthYAxis * 2 /*Adding some random offset*/); }
public void SelectIllustration(int index) { var webElementsCoverPhotos = Driver.FindElements(By.XPath($"//div[@aria-label='{ResLeftNav.ChooseAnIllustration}']//div[@aria-label='{ResLeftNav.CoverPhoto}']")); if (webElementsCoverPhotos == null || webElementsCoverPhotos.Count < index) { return; } var webElementIllustration = webElementsCoverPhotos.ToList()[index]; JscriptExecutor.ScrollToTheElement(Driver, webElementIllustration); webElementIllustration.Click(); }
private void SetIndexOfElementInViewPort() { var elementsCount = Driver.FindElements(By.XPath(BaseXPath)).Count; var index = 1; for (; index < elementsCount; index++) { // If element not in visible view port continue searching. if (!JscriptExecutor.IsElementOutViewport(Driver, GetRowXPath(index))) { continue; } CurrentRowIndex = index; return; } }
// Used this type of sloppy loops to mimic scrolling with finger. // How did I came up with these numbers, just tried bunch of things to see if it is smooth. protected void JScrollSmoothDown( string xPathDestElement) { var destWebElement = Driver.FindElementWithTimeSpan(By.XPath(xPathDestElement)); var currentLocation = ((RemoteWebElement)destWebElement).LocationOnScreenOnceScrolledIntoView; var previousLocation = currentLocation; // Slowly scroll to mimic manual scrolling. for (var i = 1; currentLocation.Y < 100; i += 80 /*80 is an offset I came up after trying different combinations*/) { for (var j = 0; j < 8; j++) { JscriptExecutor.ScrollBy(Driver, null, scrollingLengthYAxis: j * -2); } for (var j = 8; j > 0; j--) { JscriptExecutor.ScrollBy(Driver, null, scrollingLengthYAxis: j * -2); } destWebElement = Driver.FindElementWithTimeSpan(By.XPath(xPathDestElement)); currentLocation = ((RemoteWebElement)destWebElement).LocationOnScreenOnceScrolledIntoView; // Please note, this check is added to ensure when we scroll the element and // new location is same as previous location, it is an indicator that the element // we are trying to scroll is hidden by other element and did not move when using // ScrollBy function. To avoid being stuck in the the infinite loop, we added this // this check. if (previousLocation == currentLocation) { break; } } // Adding this offset to give more natural scrolling effect. for (int i = 0; i < 10; i++) { ScrollMore(null, scrollingLengthYAxis: 5); } }