Reputation: 8411
In this page, I need to click on the link Privacy Policy ,(it opens a new dialog),and I must scroll down, using Selenium, and Java.
Here is what i have tried:
WebElement element = driver.findElement(By.xpath("/html/body/div[2]"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
it does not work. It scrolls the background page instead of the active dialog.
Upvotes: 1
Views: 4403
Reputation: 193088
To invoke click()
on the link with the text Privacy Policy you simply need to induce WebDriverWait for the desired element to be clickable, then again induce WebDriverWait for the desired element to be visible and then scroll in to view and you can use the following solution:
Code Block:
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class insly_Privacy_Policy {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("https://signup.insly.com/signup");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("privacy policy"))).click();
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated((By.xpath("//div[@id='document-content']//following::div[contains(.,'Revision')]"))));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
}
}
Browser Snapshot:
Upvotes: 1
Reputation: 1
You can scroll to a location relative to the number of pixels in the plane:
JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript("window.scrollBy(0,450)", "");
Upvotes: 0
Reputation: 6184
The div
you want to scroll has a unique id and you need to get a reference to it first:
WebElement element = driver.findElement(By.id('document-content'));
Afterwards you can get the last child within that privacy policy div:
List<WebElement> children = element.findElements(By.tagName("div"));
assertTrue(children.size() > 0);
WebElement elementToScrollTo = children.get(children.size()-1);
You may now be able to scroll to elementToScrollTo
.
Upvotes: 2