Selenium : can not automate youTube 's play-pause and zoom in-out

标签 selenium xpath youtube css-selectors classname

需要自动化这个链接: -

button class="ytp-large-play-button ytp-button"aria-label="播放"

我尝试过使用 css 选择器、类名、xpath ;但它不适用于 Selenium

xpath ->  //*[@id="player_uid_334744221_1"]/div[18]/div[1]/div[2]/button 

xpath ->  (//button[@aria-label='Play'])

classname -> ytp-large-play-button

css selec -> .ytp-large-play-button.ytp-button

图片:-
enter image description here

PS:- 作为引用,您可以选择任何 youtube URL。

查看我的屏幕:-
youtube 链接和我网站的 youtube 引用 URL 链接之间的区别是。
用户需要先点击播放按钮才能播放视频,在 youtube 中,当用户点击视频时它会自动播放。
enter image description here

我认为我们需要编写代码,首先需要关注播放按钮并单击它。之后 play-pause , volume , zoom in out 按钮将显示在面板中。

enter image description here

最佳答案

识别 Play / Pause 按钮,您可以使用以下代码行:

Noe : Ensure that you are using the latest build of Selenium v3.8.1, GeckoDriver v0.19.1 and Firefox Quantum Browser.


  • Java
    package demo;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class YouTube_Play_Pause_Button 
    {
        public static void main(String[] args) 
        {
            System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
            WebDriver driver = new FirefoxDriver();
            driver.get("https://www.youtube.com/watch?v=PPS89xCgNhk&t=528s");
            new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='ytp-play-button ytp-button']"))).click();
            System.out.println("Play/Pause Button Pressed");
        }
    
    }
    

  • 控制台输出:
    Play/Pause Button Pressed
    

    关于 Selenium : can not automate youTube 's play-pause and zoom in-out,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47880663/

    相关文章:

    python - 如何使用 selenium webdriver 将现有的登录 token 用于 telegram web

    python - 在 Python Selenium 中,如何等待找到两个元素并继续循环

    python - 如何抓取这个字段?

    xpath - 使用 XPath 查找节点的位置

    java - 如何使用 XPATH 到达特定的子节点?

    java - Selenium - 如何获取元素及其子元素文本

    youtube - YouTube API 3 channel 的用户名和ID不一致

    javascript - YouTube Iframe API 无法将消息发布到 http。收件人有来源 https

    perl - 使用 Perl 进行测试自动化

    xcode - 仅在播放视频时使用横向模式。代码。 IOS 7