I am using Selenium webdriver. I have to read a xpath of link from a file and search whether the link is present on webpage, If it is present then click on it. Thats it!!
Heres the file for the links
link1 //a[contains(text(), 'Volunteer Registration')]/@href
link2 //a[contains(text(), 'Sign Up')]/@href
link3 //a[contains(text(), 'Register/sign Up')]/@href
Like wise I have one file from where I'll read one link and its associated xpath and based on that Xpath I'll search whether the link is present on webpage or not.
The Code I have written for that is :
Reading data from text file into Hashtable -
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;
}
Method to return xpath value from hashtable based on the key
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;
}
Now I want to write a method to just search whether the xpath related link is present on webpage or not..
Please help me with that..
The logic is something like
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();
}
But I am not sure about it. Please let me know if the approch is correct also if this function is approprite. Please suggest the better way to implement the code.
Thanks!!