Scrollby method in selenium - selenium

I have used the below code for scroll down.
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,1250)", "");
How do you calculate the pixel values when you want to scroll the window to a specific point ?

I guess you are trying to scroll to some element.
what you can do is:
WebElement e = driver.findElement(....);
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].scrollIntoView(true);", e);

Related

Unable to switch to iframe using Selenium Webdriver

I am trying to learn the feature drag and drop in the webpage
Link
The Section Books is inside the iframe.
But i am unable to access the iframe I am getting below error
no such element: Unable to locate element:
Below are the Xpath i tried
// WebElement frame =driver.findElement(By.xpath("//div[#id='root']//iframe[#class='st-preview-body']"));
// WebElement frame =driver.findElement(By.tagName("iframe"));
WebElement frame =driver.findElement(By.xpath("//iframe[#src='https://snippet.webixcode.com/snippet.html?0.0.3']"));
driver.switchTo().frame(frame);
I also tried using by.id and also xpath using id and class name
What will be the mistake i am doing? To my knowledge there is only one iframe present
The code for drag and drop. will this work?
WebElement fromdrag=driver.findElement(By.xpath("//span[#class='dhx_tree-list-item__text'][normalize-space()='Lawrence Block']"));
WebElement todrop=driver.findElement(By.id("treeTarget"));
Actions act=new Actions(driver);
act.dragAndDrop(fromdrag, todrop).build().perform();
Its a Nested iframe. We need to switch to those iframes one by one to access Elements from All Books.
The page takes time to load, better apply Explicit waits to find the Elements. And you also need to driver.switchTo().defaultContent(); to come out of the iframe and find Elements outside the iframe
public void iframequestion() {
System.setProperty("webdriver.chrome.driver","C:\\expediaproject\\Chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
JavascriptExecutor js = (JavascriptExecutor)driver;
driver.get("https://dhtmlx.com/docs/products/dhtmlxTree/");
WebElement frame1 = driver.findElement(By.xpath("//iframe[#src='https://snippet.dhtmlx.com/3e0b5r57?mode=mobile']"));
js.executeScript("arguments[0].scrollIntoView(true);", frame1);
driver.switchTo().frame(frame1);
WebElement frame2 = driver.findElement(By.xpath("//iframe[#class='st-preview-body']"));
driver.switchTo().frame(frame2);
WebElement frame3 = driver.findElement(By.id("content"));
driver.switchTo().frame(frame3);
WebElement ele1 = driver.findElement(By.xpath("//span[text()='Fiction & Fantasy']"));
System.out.println(ele1.getText());
driver.switchTo().defaultContent();
WebElement ele2 = driver.findElement(By.xpath("//a[text()='View more demos']"));
System.out.println(ele2.getText());
driver.quit();
}
Fiction & Fantasy
View more demos

How can selenium click the button via x,y position

I found the button's x.y position via opencv function matchTemplate(),and now how can I click the button via its x,y position?
In WebDriver, we can use Actions
new Actions(oWebDriver).moveByOffset(100, 200).click().build().perform();
where 100, 200 are x and y axis.
You are 2 ways
Scroll till view and click
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement elementToClick = driver.findElement(locator values);
jsExec.executeScript("arguments[0].scrollIntoView()", elementToClick);
jsExec.executeScript("arguments[0].click();", elementToClick);
Scroll for a specific limit and click on the element
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollBy(0, 250)", "");
WebElement elementToClick = driver.findElement(locator values);
js.executeScript("arguments[0].click();", elementToClick);

How to scroll down the page till bottom(end page) in the Selenium WebDriver

I need to scroll down page till end in the Selenium WebDriver. I tried to scroll down the page by using the following code snippet:
JavascriptExecutor jse6 = (JavascriptExecutor) driver;
jse6.executeScript("window.scrollBy(0,250)", "");
It's being scrolled but I need to scroll down till end page.
We have to use JavascriptExecutor
To scroll using coordinate
((JavascriptExecutor) driver).executeScript("window.scrollBy(0,500)");
To scroll till end of the page
((JavascriptExecutor) driver)
.executeScript("window.scrollTo(0, document.body.scrollHeight)");
To scroll till any element
((JavascriptExecutor) driver).executeScript(
"arguments[0].scrollIntoView();", element);
do it with python,
import time
time.sleep(2)
drive.execute_script("window.scrollTo(0, document.body.scrollHeight)")
based on #shubham bansal's
For this you can take the xpath of any object at the end of the page manually.And use the below code.
WebElement lastElement =
driver.findElement(By.xpath("//a[#title='org.apache.spark download']"));
int y = lastElement.getLocation().getY();
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollTo(0,"+y+")");
Thread.sleep(3000);

Selenium objects out of the screen

I currently have a selenium test that runs smooth on 1920 * 1080 resolution.
But I have a task to make this test on different common resolutions such as
1366 *768.
Problem is when I run my Selenium test on smaller resolutions than 1920 * 1080 I can't find some elements that is below the window (as expected)
How do I solve this?
I've tried
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight));");
To scroll to the bottom of the page but with no success.
Would appreciate help loads.
Using java, Selenium, TestNG and POM.
Hi to scroll please use like below
Scrolling to bottom of a page
driver.navigate().to(URL);
((JavascriptExecutor) driver)
.executeScript("window.scrollTo(0, document.body.scrollHeight)");
Scrolling to an element on a page
driver.navigate().to(URL);
WebElement element = driver.findElement(By.id("id"));
((JavascriptExecutor) driver).executeScript(
"arguments[0].scrollIntoView();", element);
Scrolling by coordinates
driver.navigate().to(URL);
((JavascriptExecutor) driver).executeScript("window.scrollBy(0,500)");
to scroll bottom of page, use the below code:
WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollBy(0,300)", "");
or
js.executeScript("scroll(0, 300);");
or
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");

I want to scroll the page

I want to scroll the page iI used following code but I need to move scrollbar manually up to 1/4th then it get scroll automatically what is the reason behind this, any new code suggestion.
Actions dragger = new Actions(driver);
WebElement draggablePartOfScrollbar = driver.findElement(By.className("mCSB_dragger_bar"));
int numberOfPixelsToDragTheScrollbarDown = 1000;
dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(10,numberOfPixelsToDragTheScrollbarDown).release().perform();
dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(10,numberOfPixelsToDragTheScrollbarDown).release().perform();
I generally use JavascriptExecutor for this purpose.
WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight," + "document.body.scrollHeight,document.documentElement.clientHeight));");