官术网_书友最值得收藏!

Understanding the text() methods

One very useful method in finding XPath is the text() method. When we need to supply some text at runtime, say for example, from an Excel file, then we can utilize the text() method in the following manner:

public class DynamicText {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:\\SeleniumWD\\src\\main\\resources\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.google.com");
String variableData = "Google";
String dynamicXpath = "http://*[text()='" + variableData + "']";
List<WebElement> elem =
driver.findElements(By.xpath(dynamicXpath));
System.out.println("no of elements: " + elem.size());

}
}

The program above prints
no of elements: 3
主站蜘蛛池模板: 合肥市| 马山县| 东乡县| 平泉县| 长武县| 毕节市| 石泉县| 霸州市| 蛟河市| 普宁市| 霍林郭勒市| 静乐县| 济源市| 泸西县| 高州市| 绥棱县| 贵德县| 股票| 长沙县| 安顺市| 恩施市| 石城县| 武乡县| 太仆寺旗| 大新县| 湘阴县| 宝丰县| 天峻县| 天津市| 无极县| 微博| 新兴县| 绿春县| 腾冲县| 师宗县| 剑川县| 九江市| 宁武县| 日土县| 泰和县| 佛冈县|