【问题标题】:XPath to CSS selector on YouTubeYouTube 上的 XPath 到 CSS 选择器
【发布时间】:2016-03-19 19:53:39
【问题描述】:

一段时间以来,我一直在尝试通过使用 selenium 自动运行视频来从 youtube 收集数据。

但是在 Edge 中运行有些麻烦导致微软不支持 Xpath, 所以我知道它支持 CSS 选择器。

因此,如果有人可以帮助我找到如何通过 CSS 更改质量,那就太好了,通过 web Microsoft web 驱动程序不支持 Python(在他们的 web 中编写,所以我使用 java)

到目前为止,我已经包含了代码,以及我是如何在 chrome 中使用 python 中的 Xpath 完成的。

以下是 Java 代码:

import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver.Window;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;


public class testEdge {

    public static void main(String[] args) {

        File file = new File("C:\\Program Files (x86)\\Microsoft Web Driver\\MicrosoftWebDriver.exe");
        System.setProperty("webdriver.edge.driver", file.getAbsolutePath());


        DesiredCapabilities capabilities = DesiredCapabilities.edge();
        EdgeDriver driver = new EdgeDriver(capabilities);

        Window window = driver.manage().window();
        window.maximize();



        driver.get("https://www.youtube.com/watch?v=pRpeEdMmmQ0");
       WebDriverWait wait = new WebDriverWait(driver,2);
       wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#movie_player > div.ytp-chrome-bottom > div.ytp-chrome-controls > div.ytp-right-controls > button.ytp-button.ytp-settings-button")));

        driver.findElementByCssSelector("#movie_player > div.ytp-chrome-bottom > div.ytp-chrome-controls > div.ytp-right-controls > button.ytp-button.ytp-settings-button").click();;

       // wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#ytp-main-menu-id > div:nth-child(4) > div.ytp-menuitem-label")));

      driver.findElement(By.cssSelector("#ytp-main-menu-id > div:nth-child(4) > div.ytp-menuitem-label")).click();;

      driver.findElement(By.cssSelector(":contains('text')")).click();;


     // driver.findElement(By.className("ytp-menu-label-secondary")).click();

        // #ytp-main-menu-id > div:nth-child(1)
    }
}

在Python中获取XPath质量的查询:

driver.find_element_by_xpath("//*[contains(text(),'"+video_quality+"')]").click()

【问题讨论】:

  • 这是你提到的哪个按钮xpath?
  • 质量,你打开播放器菜单栏选择质量,我想用CSS选择器选择的质量

标签: java python selenium css-selectors microsoft-edge


【解决方案1】:

CSS3 不支持文本属性,因此 webdriver 也不支持。除此之外,CSS 也不支持回溯。

除了所有其他的 css 定位器都可以转换为 xpath。但到目前为止,它是手动过程。您可以通过您想要 css 定位器的 xpath。我会尽力回答你的

【讨论】:

    猜你喜欢
    • 2010-12-26
    • 2012-05-28
    • 1970-01-01
    • 2012-02-18
    • 2019-12-03
    • 1970-01-01
    • 2020-05-12
    • 2014-09-01
    • 2013-06-21
    相关资源
    最近更新 更多