How to scroll with in the chrome browser window - c#

How to scroll to the bottom of the license agreement, which will enable the Accept button? The browser itself does not have a scroll bar; only the agreement has it.
I have googled and tried many different solutions and still can not move the scroll bar., at all.
C# code I've tried. I also tried many other and none is working. Anyone has any idea how to make it work?
IWebElement ScrollBar => DriverContext.Driver.FindElement(By.XPath("//div[#class='frm-scrollbar height-with-eula']"));
Actions act = new Actions(DriverContext.Driver);
act.MoveToElement(ScrollBar).ClickAndHold(ScrollBar).MoveByOffset(0, 1000).Release().Perform();
What Im trying to scroll:
Html for the scroll bar section:

Used this to scroll to the bottom of the page. This works for areas where the scroll bar is inside the webpage itself. Used this in multiple pages for scrolling.
EventFiringWebDriver ef = new(Driver);
// cssStr is the css for the area that includes the scroll bar
cssStr = "div[class=\\\"max-h-370 overflow-y-auto pl-24 pr-24\\\"]";
// 10000 or any number depending on scroll distance in pixels
ef.ExecuteScript("document.querySelector('" + cssStr + "').scrollTop = 10000");
IJavaScriptExecutor js = (IJavaScriptExecutor)DriverContext.Driver;
// by is to be constructed and passed
IWebElement ele = Driver.FindElement(by);
js.ExecuteScript("arguments[0].scrollIntoView(true);", ele);

Related

Drag and drop not working using Actions when draggable=true using Selenium and C#

Elements are identified correctly and i can see mouse moving between this two elements but drag and drop not happening.
Ui not displayed any highlights when click and hold.
No errors also.
I have tried different solutions suggested on different discussions none of them working for me
My code
_actions = new Actions(Driver.WebDriver);
var dragAndDrop = _actions.ClickAndHold(parentRow)
.MoveToElement(childRow )
.Release(target)
.Build();
dragAndDrop.Perform();
Driver.Wait();
This is how i am identifying elements
var childList =Driver.WebDriver.FindElements(By.ClassName("itl-treeNode-title"));
var parentRow = childList.FirstOrDefault(x => x.Text.Equals(parentSrc)).FindElement(By.XPath("following-sibling::*[1]"));
var childRow = childList.FirstOrDefault(x => x.Text.Equals(childSrc)).FindElement(By.XPath("following-sibling::*[1]"));
Same code works on another ui on our application.
I have now changed my code like below and now i am getting stale element exception- Since i need to identify this element dynamically i can not use the POM solution mentioned here https://www.softwaretestingmaterial.com/stale-element-reference-exception-selenium-webdriver/#How-To-Overcome-Stale-Element-Reference-Exception-in-Selenium
var childList = Driver.WebDriver.FindElements(By.ClassName("itl-treeNode-title"));
var parent = childList.FirstOrDefault(x => x.Text.Equals(parentSrc)).FindElement(By.XPath("parent::*"));
var parentRow = parent.FindElement(By.ClassName("itl-treenode-content-cover"));
var child = childList.FirstOrDefault(x => x.Text.Equals(childSrc)).FindElement(By.XPath("parent::*"));
var childRow = child.FindElement(By.ClassName("itl-treenode-content-cover"));
childRow.Click();
//try
//{
// (new Actions(Driver.WebDriver)).DragAndDrop(childRow, parent).Perform();
//}
//catch (Exception ex)
//{
// throw new Exception("Failed to perform drag and drop ");
//}
new Actions(Driver.WebDriver).ClickAndHold(childRow)
.MoveToElement(parent)
.Release(parent)
.Build()
.Perform();
Driver.Wait();
Exception
OpenQA.Selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
(Session info: chrome=77.0.3865.120)
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.PerformActions(IList`1 actionSequenceList)
at OpenQA.Selenium.Interactions.Actions.Perform()
As per the HTML you have shared the WebElement which you want to DragAndDrop() contains the attribute draggable=true
draggable
draggable is a attribute that indicates whether an element can be dragged, either with native browser behavior or the HTML Drag and Drop API. draggable can have the following values:
true: the element can be dragged.
false: the element cannot be dragged.
Note: This attribute is enumerated and not Boolean. A value of true or false is mandatory, and shorthand like <img draggable> is forbidden. The correct usage is <img draggable="false">.
If this attribute is not set, its default value is auto which means drag behavior is the default browser behavior: only text selections, images, and links can be dragged. For other elements, the event ondragstart must be set for drag and drop to work.
Native HTML5 Drag and Drop
Eric Bidelman in the article Native HTML5 Drag and Drop mentioned, making an object draggable is simple as you only need to set the draggable=true attribute on the element you want to make moveable. As an example:
<div id="cols">
<div class="col" draggable="true"><header>X</header></div>
<div class="col" draggable="true"><header>Y</header></div>
<div class="col" draggable="true"><header>Z</header></div>
</div>
To enable other types of content to be draggable you can leverage the HTML5 DnD APIs. However, using CSS3 you can spruce up the markup to look like columns and adding cursor gives users a visual indicator that something is moveable but most browsers will create a ghost image of the content being dragged and draggable won't do anything. Some browser, FF in particular will require that some data be sent in the drag operation.
Further, Remy Sharp in the article Dragging Anything mentioned:
The HTML 5 spec says it should be as simple as adding the following attributes to the markup of the elements in question:
draggable="true"
However, this doesn’t work completely for Safari or Firefox. For Safari you need to add the following style to the element:
[draggable=true] {
-khtml-user-drag: element;
}
This will start working in Safari, and as you drag it will set a default, empty value with the dataTransfer object. However, Firefox won’t allow you to drag the element unless you manually set some data to go with it.
Solution
To solve this, we need a dragstart event handler, and we’ll give it some data to be dragged around with:
var dragItems = document.querySelectorAll('[draggable=true]');
for (var i = 0; i < dragItems.length; i++) {
addEvent(dragItems[i], 'dragstart', function (event) {
// store the ID of the element, and collect it on the drop later on
event.dataTransfer.setData('Text', this.id);
});
}
Here you can find a working demo of drag and drop anything (source code)
This usecase
The realtime HTML would have helped us to analyze the issue in a better way. However you can try either of the following chained methods as solution:
Using ClickAndHold(), MoveToElement() and Release():
new Actions(Driver.WebDriver).ClickAndHold(parentRow).MoveToElement(childRow ).Release(target).Build().Perform();
Using DragAndDrop():
new Actions(Driver.WebDriver).DragAndDrop(parentRow, childRow).Build().Perform();
Note: Induce WebDriverWait to ensure that the element to be dragged is clickable.
After clicking on childRow, build the webelement again to avoid stale element reference exception, sample code below.
var childList = Driver.WebDriver.FindElements(By.ClassName("itl-treeNode-title"));
var parent = childList.FirstOrDefault(x => x.Text.Equals(parentSrc)).FindElement(By.XPath("parent::*"));
var parentRow = parent.FindElement(By.ClassName("itl-treenode-content-cover"));
var child = childList.FirstOrDefault(x => x.Text.Equals(childSrc)).FindElement(By.XPath("parent::*"));
var childRow = child.FindElement(By.ClassName("itl-treenode-content-cover"));
childRow.Click();
childRow = child.FindElement(By.ClassName("itl-treenode-content-cover"));
new Actions(Driver.WebDriver).ClickAndHold(childRow)
.MoveToElement(parent)
.Release(parent)
.Build()
.Perform();
Driver.Wait();

How to Set Page Zoom to 80% using Selenium webdriver. Using Javascript executor will reduce the Body tag only. Any other way?

I have a website which will auto hide the contents based on the resolution of the screen. All the contents of that website will be only shown if the Page Zoom is 80% and below only.
I have tried with actions, Javascript executor. Nothing works. Javascript executor is simply reduce the size of the elements inside the Body tag. So the resolution is not getting changed at all. Actions is not working at all.
new Actions(Driver)
.SendKeys(html, Keys.Control + Keys.Subtract + Keys.Null).Perform();
new Actions(Driver)
.SendKeys(html, Keys.Control + "-" + Keys.Null).Perform();
for chrome I may recommend you to try to add an option to run :
--enable-use-zoom-for-dsf
But I'm not able to check it now but it could help.
more info here:
https://peter.sh/experiments/chromium-command-line-switches/#load-extension

Why is my web page is automatically scrolling in selenium?

Am trying to access WebElement properties of a table in a webpage. Doing so is causing the page to automatically scroll down.
For Example, get The WebElement using xpath
IWebElement list = Driver.FindElement(By.XPath("//*[#id='BOM Detail Data_data_panel']/div[1]/div[1]/div[3]/table[1]"));
On Trying to access below statement the page is scrolling
bool text = list.Displayed;
How can i stop scrolling?
I don't think you can stop scrolling in WebDriver. It's how it has been designed. it does the native event rather than javascript events it used to do in Selenium RC.
For example, how would you click in element without scrolling down to that element if it's not in view port.
However, if you don't want to scroll when you try to click some element. I think you should you javascript to click on that element.
js.executeScript("document.getElementById('id').click();");

how to scroll to a position in a page by clicking on link

Hi coder i am new to jquery. i serach alot but fail to find this what i am looking is not a customize application, i just want process or method how we can achieve this.
Suppose i have a menu or link like below
Home
About
Contact
As a link or menu and when i click on that any of the link or menu i srcoll down to position of the page where details for it present..
Just like This link where click on the above menu scroll to the
position of it on the page
This should be a single page application.
Any idea's or tutorial for this i want to do it jquery.
I can use scrollup(), scroll(), animate() but how to proceed this that is the main concren any help would be appreciated.
Are you by any chance talking about HTML anchors?
http://www.echoecho.com/htmllinks08.htm
This is the best link for your solution
Single Page Site with Smooth Scrolling, Highlighted Link, and Fixed Navigation
In this link you get how to stick your menu using this
$(window).scroll(function () {
var window_top = $(window).scrollTop()+12; // the "12" should equal the margin-top value for nav.stick
var div_top = $('#nav-anchor').offset().top;
if (window_top > div_top) {
$('nav').addClass('stick');
} else {
$('nav').removeClass('stick');
}
});
use scrollto.js file to scroll through the page and call it in that function for smooth scrolling
$("nav a").click(function (evn) {
evn.preventDefault();
$('html,body').scrollTo(this.hash, this.hash);
});
I hope this will help you
$('html,body').animate({scrollTop: $('#scrollToId').offset().top});
Check this solution jQuery scroll to element

Get HTML text from page after scrolling

I am trying to capture HTML text from a page, in the case of this website more info is loaded dynamically as you scroll. So it is easy enough to capture the first page of text, but have do I get the next. I have the auto scroll part worked out fine like this:
string Java = "javascript:window.scroll(0,";//Builds up logic so screen will scroll by one page each time
string JavaScroll = Java + Scroll + ");";
webBrowser1.Navigate(JavaScroll);
Scroll += 3050;
String Morefriends = webBrowser1.DocumentText;
The problem is, even when I scroll, the Morefriends HTML text still remains the same as the first page, like it is not updating to reflect the HTML text from the new page that was just scrolled to.

Categories

Resources