5

Thank you for reviewing my query. On a webpage, there are more than 200 links & I've ensure all are working. This is easy after fetching href value but the problem is, the 'href' value doesn't contain a link rather a 'javaScript function' Here is a source

<a tabindex="8" title="Internal Crossload" target="_self" href="javascript:fnnHomePage('3' , 'WTMS_EXPRESS')">&nbsp;&nbsp;&nbsp;&nbsp;- Internal Crossload </a>

JavaScript function:

<Script>
    /*********************************************************************
    Function Name          : fnnHomePage
    Input Parameter(s)     : transferTypeId
    Output Parameter(s)    : None
    **********************************************************************/
    function fnnHomePage(transferTypeId ,moduleName) {
        if (moduleName == "XXX_EXPRESS")
     {
            document.getElementById("transferTypeId").value=transferTypeId;
            document.getElementById("gadgetType").value="XXX_EXPRESS";
            document.getElementById("moduleName").value="XXX_EXPRESS";
            document.forms[0].action="/XXX/getProposalHomePage.do?transferTypeId="+transferTypeId;
        document.forms[0].submit();
     }
        if (moduleName ==  "CROSSLOAD")
         {
            document.getElementById("transferTypeId").value=transferTypeId;
            document.getElementById("gadgetType").value="CROSSLOAD";
            document.getElementById("moduleName").value="CROSSLOAD";
            document.forms[0].action="/XXX/getCrossLoadHomePage.do?transferTypeId="+transferTypeId;
            document.forms[0].submit();
         }
    }
</Script>

From the above code, how to I get a 'Link' and check if it is working fine in selenium webdriver? There are several links & each one calls a different 'JavaScript function'.Any suggestions will be appreciated. Thank you.

8
  • The major issue is the document.form.submit() call. There is no easy way of suppressing this. But I have very limited javascript experience. If you manage it then you can iterate through all the hrefs, click on them and get the link in the form action attribute. Commented Aug 24, 2016 at 10:25
  • Here is a link that shows how to suppress it - stackoverflow.com/questions/31377500/… -- Check TJ Crowder answer. I think you will need to inject the function into a script tag using javascriptexecutor then call it. Maybe then it works. Commented Aug 24, 2016 at 10:29
  • How about using the webdriver event listener - public void beforeNavigateTo(String url, WebDriver driver) { System.out.println("Before navigating to: '" + url + "'"); }. If you add driver.navigate.back() in this maybe it works. Commented Aug 24, 2016 at 10:41
  • @Grasshopper I am unsure of how many links would be present on a webpage nor I've a visibility of URL in source code. So the approach will not apply for my situation. Commented Aug 24, 2016 at 10:54
  • You do not need to worry about number of urls or url value in source. You just need to setup a webdriver listener. The webdriver framework will call this method with the url that is automatically generated by javascript before it navgates to the page after you click the link. You just need to use the EventFiringWebDriver and create logic to maintain which link you are clicking. Google it or check this - seleniumeasy.com/selenium-tutorials/… Commented Aug 24, 2016 at 11:04

2 Answers 2

2
+25

You may use a simple trick- click on the link. It will redirect you to the new link generated by javascript function. Then, get the link using driver.getCurrentUrl(); and go back to your original page and do your stuff as usual.

I hope, this makes sense.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your response. I don't want to be moving back & forward since it is time consuming.
then you can open all the links in new tabs and iterate throught them wile getting the their URLs. it would be faster then moving back & forward and you will be able to check links' validity.
-1

Here is the working code,using this code you can see all the links on your console and further you can navigate to all the links:

 driver.get("https://www.facebook.com");
    List<WebElement> all_links_webpage = driver.findElements(By.tagName("xyz")); 
    System.out.println("Total no of links Available: " + all_links_webpage.size());
    int links = all_links_webpage.size();
    System.out.println("List of links Available: ");
    for(int i=0;i<links;i++)
    {
    if(all_links_webpage.get(i).getAttribute("href").contains("google"))
    {
    String link = all_links_webpage.get(i).getAttribute("href");
    System.out.println(link);
    }   
    }

Hope this will solve your problem.

2 Comments

Thank you for quick response. This will work if 'href=www.google.com/mail' but in this case, a javascript function is called. This will not fetch the URL rather print javascript function name on console.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.