如何使用selenium web驱动程序自动化youtube?

时间:2018-05-15 12:50:09

标签: java selenium-webdriver

我只是尝试自动下载YouTube视频'使用硒网络驱动程序。但它不起作用。在这里,我只是打开YouTube视频并复制其网址。然后我在' SaveItOffline'中粘贴该网址。选项卡在另一页上,然后单击下载。但是不会复制URL。这是我的代码,

    import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;`enter code here`
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Keys;
public class Download {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
    System.setProperty("webdriver.chrome.driver","C:/Users/vairavan/Desktop/SELENIUM/chrome driver/chromedriver.exe");
    WebDriver driver=new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    driver.get("http://www.youtube.com");
    driver.findElement(By.id("search")).click();
    driver.findElement(By.id("search")).sendKeys("short flute cover");
    driver.findElement(By.id("search")).sendKeys(Keys.ENTER);
    driver.findElement(By.id("dismissable")).click();
    Actions action=new Actions(driver);
    action.contextClick(driver.findElement(By.xpath("/html/body/ytd-app/div[1]/ytd-page-manager/ytd-watch/div[2]/div[1]/div[1]/ytd-player/div/div[1]/video"))).perform();
    driver.findElement(By.xpath("/html/body/div[6]/div/div/div[1]/div[1]")).click();
    driver.get("htttp://www.saveitoffline.com/");
    driver.findElement(By.id("url-input")).sendKeys(Keys.CONTROL+"v");
    driver.findElement(By.id("submit-url")).click();
    driver.findElement(By.xpath("/html/body/section/div[3]/div/div[1]/div[2]/div")).click();
    driver.findElement(By.id("download-href")).click();

    }



}

1 个答案:

答案 0 :(得分:0)

请使用剪贴板粘贴网址,这对我有用:

Toolkit toolkit = Toolkit.getDefaultToolkit();
Clipboard clipboard = toolkit.getSystemClipboard();
String data = (String) clipboard.getData(DataFlavor.stringFlavor);
webDriver.findElement(By.id("url-input")).sendKeys(data);