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

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
主站蜘蛛池模板: 晋宁县| 固安县| 揭阳市| 保靖县| 阿拉善左旗| 樟树市| 大同县| 察雅县| 西华县| 乃东县| 仁布县| 满洲里市| 吉首市| 白河县| 东阳市| 会昌县| 五华县| 石河子市| 房产| 潜山县| 长汀县| 旌德县| 通州市| 尉氏县| 明溪县| 全州县| 陕西省| 白玉县| 怀集县| 通许县| 石台县| 徐水县| 夏河县| 清新县| 吉安市| 潞城市| 桑日县| 醴陵市| 临泉县| 托克逊县| 本溪市|