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

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
主站蜘蛛池模板: 泊头市| 绩溪县| 九龙城区| 杂多县| 抚顺县| 沂水县| 赞皇县| 苏尼特右旗| 尉犁县| 泊头市| 肇州县| 益阳市| 湘潭县| 连江县| 法库县| 利津县| 芮城县| 阿克苏市| 寻乌县| 慈溪市| 维西| 布拖县| 南澳县| 东阳市| 小金县| 杭锦后旗| 乌兰浩特市| 乌兰察布市| 松原市| 紫金县| 武清区| 阳城县| 津市市| 横峰县| 嘉义市| 库伦旗| 丰顺县| 台江县| 罗甸县| 共和县| 泰顺县|