如何在SeleniumJavaWebDriver中获取要在运行时验证的文本的类名

如何在SeleniumJavaWebDriver中获取要在运行时验证的文本的类名,java,selenium,webdriver,xpath,Java,Selenium,Webdriver,Xpath,我需要获取并打印文本“Selenium Java”的类名Class=“community wrapper bg blue dark”,比如wise for Selenium Webdriver、Selenium Demo和Selenium Learning String classname = driver.findElement(By.xpath("//*contains[@text() = "Selenium Java")).getAttribute("class"); System.ou

我需要获取并打印文本“Selenium Java”的类名
Class=“community wrapper bg blue dark”
,比如wise for Selenium Webdriver、Selenium Demo和Selenium Learning

String classname = driver.findElement(By.xpath("//*contains[@text() = "Selenium Java")).getAttribute("class");

System.out.println(classname);
  • 在控制台中打印SeleniumJava文本前面的所有类名所需的帮助
  • 帮助需要在控制台中打印特定的类名
    class=“community wrapper bg blue dark”
  • HTML:

    
    硒爪哇

    Selenium Web驱动程序 硒演示

    硒学习

    使用以下代码:

    List<WebElement> itemList = driver.findElements(By.xpath("//div/*[@data-aos='fade-up']"));
    for (WebElement element : itemList) {
            if (element.getText().contains("Selenium Java")) {
                System.out.println("Selenium Java" + element.getAttribute("class"));
            } else if (element.getText().contains("Selenium Webdriver")) {
                System.out.println("Selenium Webdriver" + element.getAttribute("class"));
            } else if (element.getText().contains("Selenium Demo")) {
                System.out.println("Selenium Demo" + element.getAttribute("class"));
            } else if (element.getText().contains("Selenium Learning")) {
                System.out.println("Selenium Learning" + element.getAttribute("class"));
            } else {
                System.out.println("not expected label" + element.getText());
            }
    }
    
    List itemList=driver.findElements(By.xpath(“//div/*[@data aos='fade-up']);
    for(WebElement:itemList){
    if(element.getText()包含(“Selenium Java”)){
    System.out.println(“SeleniumJava”+element.getAttribute(“类”);
    }else if(element.getText()包含(“Selenium Webdriver”)){
    System.out.println(“SeleniumWebDriver”+element.getAttribute(“class”);
    }else if(element.getText()包含(“Selenium演示”)){
    System.out.println(“Selenium Demo”+element.getAttribute(“class”);
    }else if(element.getText()包含(“Selenium学习”)){
    System.out.println(“Selenium Learning”+element.getAttribute(“class”);
    }否则{
    System.out.println(“非预期标签”+元素.getText());
    }
    }
    
    要打印与文本Selenium Webdriver、Selenium Demo和Selenium Learning相关的class属性值,您需要为
    ElementLocated()的可见性引入WebDriverWait,并且您可以基于以下内容使用:

    • 使用Selenium Java文本:

    • 使用文本Selenium Web驱动程序:

    • 使用文本Selenium演示:

    • 使用Selenium学习文本:


    Selenium Java

    续:Selenium Webdriver Selenium Demo

    继续:Selenium Learning

    请帮助获取类名。我需要通过传递文本Selenium Java作为输入来获取类名。请您帮忙。@KrishnamuthyTM签出更新的答案并告诉我状态。
    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//i[text()='Selenium Java']//ancestor::div[3]"))).getAttribute("class"));
    
    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h6[normalize-space()='Selenium Webdriver']//ancestor::div[3]"))).getAttribute("class"));
    
    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h5[normalize-space()='Selenium Demo']//ancestor::div[3]"))).getAttribute("class"));
    
    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//p[normalize-space()='Selenium Learning']//ancestor::div[3]"))).getAttribute("class"));