如何使用Webdriver和javascript滚动子元素?

时间:2013-12-18 12:03:20

标签: java javascript selenium-webdriver

我希望能够滚动个人YouTube页面的订阅列表,我该怎么做?我编写的代码允许我滚动主页,有关如何调整代码的任何想法,以滚动登录YouTube时出现的YouTube页面的“我的订阅”部分?

package Check;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class java {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        FirefoxDriver driver = new FirefoxDriver();
        driver.get("https://www.youtube.com");
        Thread.sleep(2500);
        driver.findElement(By.xpath("//button[contains(.,'Sign in')]")).click();
        Thread.sleep(1500);
        driver.findElementById("Email").sendKeys("<My username>");
        driver.findElementById("Passwd").sendKeys("<My password>");
        driver.findElementById("signIn").click();
        //driver.findElement(By.xpath("//button[contains(.,'Sign in')]")).click();
        Thread.sleep(3500);
        driver.findElementByCssSelector("div[id='identity-prompt-account-list'] > ul > label + label").click();
        driver.findElementById("identity-prompt-confirm-button").click();
        Thread.sleep(2500);
        JavascriptExecutor js = (JavascriptExecutor)driver;
        js.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight," +
        "document.body.scrollHeight,document.documentElement.clientHeight));");
        Thread.sleep(5000);
        driver.quit();
    }
}

1 个答案:

答案 0 :(得分:1)

我通过一种反复试验的方法找到了一种有效的方法,CSS选择器似乎仅限于找到第35个兄弟,所以鉴于这个限制,这里是我想出的代码对我想要完成的事情非常有效。以下是我的脚本:

package Check;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.firefox.FirefoxDriver;

public class java2 {

    public static void main(String[] args) throws InterruptedException {
        FirefoxDriver driver = new FirefoxDriver();
        driver.get("https://www.youtube.com");
        Thread.sleep(2500);
        driver.findElement(By.xpath("//button[contains(.,'Sign in')]")).click();
        Thread.sleep(1500);
        driver.findElementById("Email").sendKeys("<User name>");
        driver.findElementById("Passwd").sendKeys("<Password>");
        driver.findElementById("signIn").click();
        Thread.sleep(4000);
        driver.findElementByCssSelector("div[id='identity-prompt-account-list'] > ul > label + label").click();
        driver.findElementById("identity-prompt-confirm-button").click();
        Thread.sleep(3000);
        JavascriptExecutor js = (JavascriptExecutor)driver;
        int i =0;
        String CSSText = "ul[id='guide-channels'] > li";
        do {
            if (driver.findElementByCssSelector(CSSText).getText().equals("Josie Outlaw")){
                break;
            }
            CSSText = CSSText + " + li";
            i++;
        } while (i<35);
        js.executeScript("document.getElementsByClassName(\"vve-check overflowable-list-item guide-channel\")["+i+"].scrollIntoView(false);");
        Thread.sleep(1500);
        js.executeScript("document.getElementsByClassName(\"vve-check overflowable-list-item guide-channel\")["+i+"].scrollIntoView(true);");
        Thread.sleep(10000);
        driver.quit();
    }

}