【问题标题】:How to perform motion detect using selenium webdriver?如何使用 selenium webdriver 执行运动检测?
【发布时间】:2014-06-26 15:42:45
【问题描述】:

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

【问题讨论】:

    标签: selenium selenium-webdriver automation flash


    【解决方案1】:
    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
      

    要运行这段代码,只需像这样实例化它:

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

    【讨论】:

    • 感谢鹈鹕的回复。实际上这件事我已经尝试过了,但这并不能保证视频实际上已经开始。这段代码在积极的情况下工作得很好,但在消极的情况下就不行了。
    • 我认为 selenium webdriver 不支持 flash。但是您可以尝试使用 Sikuli 进行 UI 自动化。您可以在视频的开头、中间和结尾检查一些图像。缺点是您必须为每个视频手动保存 3 张图像。然后使用 Thread.sleep() 等待视频流到一半,然后使用 Sikuli 检查预期的图像。这是一种蛮力的做法,但它会起作用。如果您能够验证预期图像出现在给定 youtube 视频的开头、中间和结尾处,那么您可以得出正确加载的视频。
    • 你可以纠正我可以使用 sikuli。但我希望我的脚本足够通用,而不依赖于屏幕的分辨率或大小。无论如何,我会提出你的答案,因为它会起作用,但它不是我想要的解决方案。
    • 感谢user3687223,这是一个youtube视频教程,我偶然发现并想到了您的问题;我不确定他是不是认真的,但视频中的那个人声称能够使用 Selenium Webdriver 自动生成基于 Flash 的视频,例如 youtube 视频;这是链接:youtube.com/watch?v=z65VETxBaEM。这是同一个人关于同一主题的另一个视频:youtube.com/watch?v=1j9GqibXeyw
    猜你喜欢
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多