[英]How to get href value in selenium webdriver when javascript is called upon click of a link?
Thank you for reviewing my query. 感谢您查看我的查询。 On a webpage, there are more than 200 links & I've ensure all are working.
在一个网页上,有超过200个链接,我确保所有链接都在工作。 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 获取
href
值后这很容易,但问题是,'href'值不包含link
而是'javaScript函数'这是一个源代码
<a tabindex="8" title="Internal Crossload" target="_self" href="javascript:fnnHomePage('3' , 'WTMS_EXPRESS')"> - Internal Crossload </a>
JavaScript function: JavaScript函数:
<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? 从上面的代码,我如何获得' 链接 '并检查它是否在selenium webdriver中正常工作? There are several links & each one calls a different 'JavaScript function'.Any suggestions will be appreciated.
有几个链接,每个链接称为不同的“JavaScript功能”。任何建议将不胜感激。 Thank you.
谢谢。
You may use a simple trick- click on the link. 您可以使用简单的技巧点击链接。 It will redirect you to the new link generated by javascript function.
它会将您重定向到javascript函数生成的新链接。 Then, get the link using
driver.getCurrentUrl();
然后,使用
driver.getCurrentUrl();
获取链接driver.getCurrentUrl();
and go back to your original page and do your stuff as usual. 然后回到原来的页面,像往常一样做你的东西。
I hope, this makes sense. 我希望这是有道理的。
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. 希望这能解决你的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.