如何使用 ClassName 识别元素及其使用 Java 的值

How to identify element using ClassName and its Value using Java

HTML:

<div class="MuiGrid-root MuiGrid-grid-xs-12 karutaNameNam css-1uqhpru">Super Heros</div>

如何 select 使用 class = karutaNameNam 及其值 = "Super Heros" 的元素。

我们可以在 class name inside 'cssSelector' 之前使用点 [.] 来使用 className inside 'findElement' method

WebElement element = driver.findElement(By.cssSelector('.MuiGrid-root.MuiGrid-grid-xs-12.karutaNameNam.css-1uqhpru'));

WebElement element = driver.findElement(By.cssSelector('.karutaNameNam'));

要将带有文本的元素标识为 Super Heros,您可以使用以下任一方法 :

  • cssSelector:

    WebElement element = driver.findElement(By.cssSelector("div[class*='MuiGrid-root'][class*='karutaNameNam']"));
    
  • xpath:

    WebElement element = driver.findElement(By.xpath("//div[contains(@class, 'karutaNameNam') and text()='Super Heros']"));
    

最好确定你需要诱导的元素 for the and you can use either of the following :

  • cssSelector:

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[class*='MuiGrid-root'][class*='karutaNameNam']][value='emptyAssembly']")));
    
  • xpath:

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(@class, 'karutaNameNam') and text()='Super Heros']")));