使用java中的selenium webdriver自动化YouTube视频时出错

时间:2015-08-25 13:02:36

标签: youtube automation

尝试使用java中的selenium webdriver在youtube中自动化视频时出现以下错误。

我已从以下链接复制代码以自动执行。

  

https://seleniumonlinetrainingexpert.wordpress.com/2012/12/03/how-to-automate-youtube-using-selenium-webdriver/

以下是我得到的错误

  

线程中的异常" main" org.openqa.selenium.WebDriverException:document.movi​​e_player未定义   命令持续时间或超时:23毫秒   构建信息:版本:' 2.46.0',修订版:' 87c69e2',时间:' 2015-06-04 16:17:10'   系统信息:主持人:' HYDPCM99232L',ip:' 10.1.1.3',os.name:' Windows 7',os.arch:' x86',os.version:' 6.1',java.version:' 1.7.0_79'   会议ID:a573f5f2-29c4-4b62-a5f2-54e44a762547   驱动程序信息:org.openqa.selenium.firefox.FirefoxDriver   Capabilities [{platform = WINDOWS,acceptSslCerts = true,javascriptEnabled = true,cssSelectorsEnabled = true,databaseEnabled = true,browserName = firefox,handlesAlerts = true,nativeEvents = false,webStorageEnabled = true,rotating = false,locationContextEnabled = true,applicationCacheEnabled = true ,takeScreenshot = true,version = 40.0.2}]       at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)       at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)       at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)       at java.lang.reflect.Constructor.newInstance(Constructor.java:526)       在org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)       在org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)       在org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:605)       在org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:514)       在FlexWebDriver.callFlashObject(FlexWebDriver.java:23)       在Youtube.main(Youtube.java:17)   引起:org.openqa.selenium.WebDriverException:document.movi​​e_player未定义   构建信息:版本:' 2.46.0',修订版:' 87c69e2',时间:' 2015-06-04 16:17:10'   系统信息:主持人:' HYDPCM99232L',ip:' 10.1.1.3',os.name:' Windows 7',os.arch:' x86',os.version:' 6.1',java.version:' 1.7.0_79'   驱动程序信息:driver.version:未知       at .anonymous(..... youtube link ...)

我是否遗漏了安装/配置以自动播放YouTube视频的内容? 这让我疯狂了2天。

1 个答案:

答案 0 :(得分:1)

我解决了问题,代码看起来像

import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.testng.annotations.Test;

    public class YouTube {

        @Test
        public void test1() throws InterruptedException {
            //Initialising the firefox driver
            FirefoxDriver driver = new FirefoxDriver();

            driver.manage().window().maximize();
            //navigating to the below url 
            driver.get("https://www.youtube.com/watch?v=qxoXvn2l2gA");

            Thread.sleep(5000L);

            WebElement video = driver.findElement(By.tagName("video"));
            JavascriptExecutor js = driver;

             //pausing the video
            js.executeScript("arguments[0].pause();", video);

            Thread.sleep(5000L);

//playing the video         
js.executeScript("arguments[0].play();", video);
            Thread.sleep(5000L);

//muting the video          
js.executeScript("arguments[0].mute();", video);
            Thread.sleep(5000L);

//unmuting the video            
js.executeScript("arguments[0].unMute();", video);
            Thread.sleep(5000L);

//Dragging the video            
js.executeScript("arguments[0].currentTime = 600;", video);
            Thread.sleep(5000L);

            js.executeScript("alert(arguments[0].readyState);", video);

            driver.quit();

        }

    }