如何使用selenium webdriver进行运动检测?

时间:2014-06-17 14:14:15

标签: selenium selenium-webdriver automation flash

所以我想自动播放YouTube视频。所以可用的API列表以及selenium都支持flash对象。我担心的是,如何检查视频是否正在播放?就像我可以在视频上执行运动检测一样,我可以相应地传递或失败脚本。那么我们可以用硒来实现类似吗?或硒有不同的方法来做到这一点。非常感谢

1 个答案:

答案 0 :(得分:0)

  1. 我会检查当他们点击“播放”按钮时,它会变为“暂停”图标;像这样的东西:

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Set;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.interactions.Actions;
    import org.openqa.selenium.support.PageFactory;
    import org.openqa.selenium.support.ui.Select;
    
    
    
    public class Youtube {
    public WebDriver driver;
    private String url = "https://www.youtube.com/watch?v=MfhjkfocRR0";
    
        public Youtube() {
        System.setProperty("webdriver.chrome.driver",     "C:\\SeleniumServer\\chromedriver.exe");
    driver = new ChromeDriver();
    //driver=new FirefoxDriver();
    driver.manage().window().maximize();
    driver.get(url);
    }
    
    public void waitFor(int wait){
         try {
        Thread.sleep(wait);
        } catch (InterruptedException e) {
        e.printStackTrace();
        }
    }
    
    public boolean isYoutubePlaying(){
    try {
        //wait 4 secs for Youtube video to load
        waitFor(6000);
        WebElement     playPauseButton=driver.findElements(By.cssSelector("div.ytp-button")).get(0);
        //video is not playing here.
        if(playPauseButton.getAttribute("aria-label").equals("Play")){
            System.out.println("This Youtube video wasn't playing but we clicked on it to play the video.");
            //so we click on the play button to play the video then we return true;
            playPauseButton.click(); 
            return true;
        }else{
            //video should be playing but let's double-check
            if(playPauseButton.getAttribute("aria-label").equals("Pause")){
                System.out.println("Youtube video is already playing.");
                return true;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    //only return false if either of the 2 cases above fail.
    return false;
     }
    
    
    }//end class
    
  2. 要运行此代码,只需将其实例化为:

        Youtube yt=new Youtube();
        yt.isYoutubePlaying();