- Selenium WebDriver Quick Start Guide
- Pinakin Chaubal
- 95字
- 2021-06-24 18:26:30
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
推薦閱讀
- Mastering vRealize Operations Manager(Second Edition)
- Social Media Mining with R
- Designing Purpose:Built Drones for Ardupilot Pixhawk 2.1
- Linux實戰
- FreeRTOS實時內核應用指南
- Extending Puppet
- 嵌入式系統原理及開發
- NetDevOps入門與實踐
- Windows 7實戰從入門到精通
- UI設計手繪表現從入門到精通
- OpenStack Essentials(Second Edition)
- Learn OpenShift
- Linux深度攻略
- BuddyPress Theme Development
- Website Development with PyroCMS