【发布时间】:2014-06-26 15:42:45
【问题描述】:
所以我想自动化 youtube 视频。所以可用的 API 列表和 selenium 都支持 flash 对象。我担心的是如何检查视频是否正在播放?就像我可以对视频执行运动检测一样,我可以相应地通过或失败脚本。那么我们可以使用硒实现类似的效果吗?或硒有不同的方法来做到这一点。非常感谢
【问题讨论】:
标签: selenium selenium-webdriver automation flash
所以我想自动化 youtube 视频。所以可用的 API 列表和 selenium 都支持 flash 对象。我担心的是如何检查视频是否正在播放?就像我可以对视频执行运动检测一样,我可以相应地通过或失败脚本。那么我们可以使用硒实现类似的效果吗?或硒有不同的方法来做到这一点。非常感谢
【问题讨论】:
标签: selenium selenium-webdriver automation flash
我会检查当他们点击“播放”按钮时,它会变成“暂停”图标;像这样:
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();
【讨论】: