我正在使用Selenium webdriver。 我必须从文件中读取链接的xpath并搜索链接是否存在于网页上,如果存在,请点击它。 这就是它!搜索网页上的链接,然后点击它使用硒
继承人的链接
link1 //a[contains(text(), 'Volunteer Registration')]/@href
link2 //a[contains(text(), 'Sign Up')]/@href
link3 //a[contains(text(), 'Register/sign Up')]/@href
就像聪明我从那里我会读一个环节及其相关联的XPath,并基于该Xpath的一个文件,我会搜索该文件是否存在链路上网页或不是。我曾经为写
准则是:从文本文件
读取数据到哈希表 -
public HashMap<String, String> readDataFromFile(String fileName) {
try {
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
String strLine = null;
String[] prop = null;
while ((strLine = br.readLine()) != null) {
prop = strLine.split("\t");
recruiters.put(prop[0], prop[1]);
}
br.close();
fr.close();
} catch (Exception exception) {
System.out.println("Unable to read data from recruiter file: " + exception.getMessage());
}
return recruiters;
}
方法返回基于关键
public String findValue(String Name){
for (String s: HashTable.entrySet()) {
Map.Entry entry = (Map.Entry) s;
if(entry.getKey().equale(Name))
{
String value= entry.getValue();
return value;
}
}
return null;
}
从哈希表的XPath值现在我想写一个方法来搜索xpath相关链接是否存在于网页上或不是。
请帮我的..
的逻辑是一样的东西
public void Search&ClickLink()
{
List<WebElement> links = driver.findElements(By.tagName("a"));
System.out.println(links.size());
for (WebElement myElement : links){
String link = myElement.getText();
System.out.println(link);
myElement.click();
driver.navigate().back();
}
但我不知道这件事。 请让我知道,如果这个功能是approprite,如果该接近是正确的。 请提出更好的实现代码的方法。
谢谢!
但问题是我该如何搜索基于其Xpath的链接?我是自动化新手,所以请你帮我写下最后一个功能。我明白了这一点,但从文件读取xpath和检查网页上的链接似乎有点困难 –
XPath不应该被要求。 By.linkText()会找到这个元素。如果你绝对需要XPath,谷歌搜索会让你很好:http://stackoverflow.com/questions/915338/xpath-find-link-url-by-link-text –