0

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);
    }
}
Alx_Wil95
  • 97
  • 1
  • 2
  • 9
  • Would the methods outlined here be applicable to your question? https://stackoverflow.com/questions/3401343/scroll-element-into-view-with-selenium – kpie Feb 18 '22 at 19:56
  • @kpie yes. I was trying something similar before my given code above, however it was unable to find any locator I used due to selenium not registering that it was on a new page. so it would try to find the given locator on the page that was opened originally rather then the page the link brings the user to – Alx_Wil95 Feb 18 '22 at 20:01

1 Answers1

0

Drop the part js = and you should be good to go.

Effectively, the line of code will be:

((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", element);
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • so after trying this it was unsuccessful. 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 – Alx_Wil95 Feb 18 '22 at 22:09