从选择ul中获取getText

时间:2019-02-20 17:12:49

标签: java selenium selenium-webdriver drop-down-menu

HTML code image

我需要li的getText来验证正确的选项是否可见。 我尝试了3种不同的选择,但它们均无法正常工作。 我需要“ boundlist-selected”类获取文本,这将验证所选文本。 在代码内部注释掉输出。

public void DDList(WebDriver driver, String actualViews,String ReportName) throws InterruptedException {

    waitForElementPresent(driver, 30, expand_btn1);
    Thread.sleep(1000); 
    By ddlselect = By.xpath("//div[input[@name='"+ReportName+"']]/following-sibling::div");
    waitForElementPresent(driver, 30, ddlselect,ReportName);
    click(driver,ddlselect,ReportName);
    Thread.sleep(2000);
    waitForLoad(driver, 60);
    By ddlexpand = By.xpath("//ul/li[text()='"+actualViews+"']");
    waitForElementPresent(driver, 30, ddlexpand ,actualViews);
    click(driver,ddlexpand,actualViews);
    Thread.sleep(2000);
    //try 1 
    WebElement textlabel = driver.findElement(By.xpath("//li[contains(@class,'boundlist-selected')]"));
    value = textlabel.getAttribute("value");
    System.out.println("getAttribute value" + value);  // value Ouput 0
    //try 2
    String textlabel2 = driver.findElement(By.xpath("//li[contains(@class,'boundlist-selected')]")).getText();
    System.out.println(textlabel2 + "print text");  // blank output 
    // try 3 
    List<WebElement>allProduct = driver.findElements(By.xpath("//ul[@class='x-list-plain']/li"));

    for( WebElement product : allProduct){
        System.out.println(product.getText()); 
        // This print correctly all 3 value now I need to validate select  text how can I do that? 
    }
// below code to validate select gets fail as value is blank 
    if (value.equals(actualViews)) {
        Reporter.log(
                "Successfully able to select " + actualViews + "option from dashboard" +ReportName+  "summary screen");
        Add_Log.info(
                "Successfully able to select " + actualViews + "option from dashboard" +ReportName+  "summary screen");
    } else {
        Assert.fail();
        Reporter.log("Not able to select " + actualViews + "option from dashboard" +ReportName+  "summary screen");
        Add_Log.info("Not able to select " + actualViews + "option from dashboard" +ReportName+  "summary screen");
    }

1 个答案:

答案 0 :(得分:0)

请尝试以下代码:

//try 1 
String selectedText = driver.findElement(By.cssSelector("li.x-boundlist-selected")).getText();
System.out.println(selectedText)

//try 2
selectedText = driver.findElement(By.cssSelector("li.x-boundlist-selected")).getAttribute("textContent");
System.out.println(selectedText)