需要自动化这个链接: -
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
图片:-
PS:- 作为引用,您可以选择任何 youtube URL。
查看我的屏幕:-
youtube 链接和我网站的 youtube 引用 URL 链接之间的区别是。
用户需要先点击播放按钮才能播放视频,在 youtube 中,当用户点击视频时它会自动播放。
我认为我们需要编写代码,首先需要关注播放按钮并单击它。之后 play-pause , volume , zoom in out 按钮将显示在面板中。
最佳答案
识别 Play
/ Pause
按钮,您可以使用以下代码行:
Noe : Ensure that you are using the latest build of
Selenium v3.8.1
,GeckoDriver v0.19.1
andFirefox 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/