0

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!!

Priya Pawar
  • 21
  • 1
  • 8

1 Answers1

0

Well...there are a couple of problems with that last set of code.

The first is that you are going to get StaleElementReferences. When you find an element (or a list of elements), they are pointing to a element to a page. If you refresh the page or leave and come back, they will not be valid, and you have to re-find all of your elements.

Also, many times a link doesn't navigate you to a new page. If this is the case with any of your links, you will suddenly find yourself clicking links on the wrong page (because you navigated back)

Finally, you aren't actually doing anything on the page. For all you know, the link could go to a 500 Error, and Selenium would have no idea.

However, since you have all of the links in a file, why not just read the file, store it in an array, and then do a simple for loop:

for (String linkName: allLinks){
   driver.get(urlWithLinks);
   driver.findElement(By.linkText(linkName)).click();
   ...validate the page...
}

Lastly...I personally believe that clicking on all of the links on a page is a terrible test. A much better test would be to go to the link, and the do stuff on the page. That way you are actually testing the functionality of the website.

Nathan Merrill
  • 7,648
  • 5
  • 37
  • 56
  • But the question is how should I search the link based on its Xpath? I am new in automation so, Could you please help me write this last function. I understood the approch but the xpath reading from file and checking the link on webpage seems bit difficult to me – Priya Pawar Aug 20 '13 at 18:37
  • XPath shouldn't be required. The `By.linkText()` will find the element. If you absolutely demand XPath, a google search would have done you good: http://stackoverflow.com/questions/915338/xpath-find-link-url-by-link-text – Nathan Merrill Aug 20 '13 at 21:16