如何从以下代码中的span获取文本

时间:2016-08-22 12:17:57

标签: selenium checkbox dropdown

regiondropdown Unable to save UI/web Page我想从下拉框中的所有复选框中获取文本,以便将这些值与db进行比较。请帮我。我可以通过options.size()来计算大小,但无法将文本/字符串与db中的每个字符串进行比较。

<template name="test1">
   <div id="test2">
   Hello World
   </div>
  {{helloworld}}
</template>

debug

3 个答案:

答案 0 :(得分:0)

现在,每个“选项”都是一个“输入”WebElement,它不会有任何文本。 您可能想尝试搜索列表项,然后拉出范围内容:

List<WebElement> options = driver.findElements(By.cssSelector("ul.ui-multiselect-checkboxes li label[for*='ddlZone'] span"));

答案 1 :(得分:0)

如果遗憾的是getText()不起作用,您应该尝试使用getAttribute("textContent"‌​),如下所示: -

System.out.println(options.get(i).getAttribute("textContent"‌));

或尝试使用getAttribute("innerHTML"‌),如下所示: -

System.out.println(options.get(i).getAttribute("innerHTML"‌));

答案 2 :(得分:0)

尝试“www.tripadvisor.in”,并且能够制作它(使用'break'语句只是为了限制示例中的循环)。以下是我试过的代码

public class SelectList {
WebDriver driver;

By currencyDrop = By.xpath(".//*[@id='CURRENCYPOP']/span");
By currency = By.xpath(".//*[@id='GLOBAL_CURRENCY_PICKER']/ul/li");

@Test 
public void f() throws InterruptedException { 

driver = new FirefoxDriver();  
driver.get("https://www.tripadvisor.in/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

driver.findElement(currencyDrop).click();

//Below is the list of options present in dropdown(per DB)
 String[] expList = {"INR ₹ Indian Rupees", 
                     "USD US$ U.S. Dollars", 
                     "EUR € Euros"};

List <WebElement>countryList = driver.findElements(currency);

int i =0;

for(WebElement curr:countryList){
    if (i>2){
        break;
    }
    if (curr.getText().equals(expList[i])){
        System.out.println("ActCurrency" + curr.getText());
        System.out.println("ExpCurrency" + expList[i]);
i++;
    }
}
}
}
相关问题