I have a page I am testing for practice and in that page I have a link I need to click on and navigate to then scroll to a specific element on the new page. when the test runs everything passes but the page does not scroll to the element in question. can someone explain please?
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.util.Iterator;
import java.util.Set;
public class AspirationBase {
public WebDriver driver;
public JavascriptExecutor js;
public WebElement element;
public String URL = "https://www.aspiration.com";
public String xpath;
public String handleWindow;
public Set<String> allWindows;
public Iterator<String> iterator;
public int sleepTime = 2000;
public AspirationBase(WebDriver driver) {
this.driver = driver;
}
public void launchAspiration() throws InterruptedException {
driver.get(URL);
Thread.sleep(sleepTime);
}
}
package searchaspiration;
import aspirationbase.AspirationBase;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
public class SearchAspiration extends AspirationBase {
public SearchAspiration(WebDriver driver) {
super(driver);
}
public void viewProducts() throws InterruptedException {
handleWindow = driver.getWindowHandle();
xpath = "//*[@id=\"__next\"]/div/header/ul[1]/li[1]/a";
driver.findElement(By.xpath(xpath)).click();
Thread.sleep(sleepTime);
allWindows = driver.getWindowHandles();
iterator = allWindows.iterator();
while (iterator.hasNext()) {
String newURL = iterator.next();
if (!handleWindow.equalsIgnoreCase(newURL)) {
driver.switchTo().window(newURL);
xpath = "//*[@id=\"__next\"]/div/section[7]/div/div[1]/div[1]";
element.findElement(By.xpath(xpath));
(js = (JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", element);
}
}
}
}
Edit: Before the given code I tried simply navigating to the link and scrolling to element but selenium could not recognize the given locator since it was trying to find it in the original page opened. Now it seems like it's just ignoring my while loop after the first few tests are ran. I'm more so trying to find an explanation as to where I'm going wrong rather than a direct answer so as to better understand
Edit1: After reading some more documentation I realize the problem is everything in the while loop is trying to find a window that is already open which I do not want. what I am trying to accomplish is clicking on a link in the first page that leads to a new page and then scroll down the new page to the given element. As stated before simply using .click() to navigate the like and then using the elements xpath on new page does not work as it's trying to find xpath on previous page. any suggestion on how I can identify that xpath would be great. this is w/o while loop:
import aspirationbase.AspirationBase;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
public class SearchAspiration extends AspirationBase {
public SearchAspiration(WebDriver driver) {
super(driver);
}
public void viewProducts() throws InterruptedException {
xpath = "//*[@id=\"__next\"]/div/header/ul[1]/li[1]/a";
driver.findElement(By.xpath(xpath)).click();
Thread.sleep(sleepTime);
xpath = "//*[@id=\"__next\"]/div/section[7]/div/div[1]";
element.findElement(By.xpath(xpath));
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", element);
Thread.sleep(sleepTime);
}
}