简体   繁体   English

如何在点击链接时调用javascript时获取selenium webdriver中的href值?

[英]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')">&nbsp;&nbsp;&nbsp;&nbsp;- 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. 希望这能解决你的问题。

Selenium,WebDriver:获取元素,“<div id="text_translate"><p> javascript 获取元素的按钮源码: </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> <a href="JavaScript:onclick=bookProsecc('20220512','1550','A','B','0','','')"> <img src="/image/cal_app.jpg" alt="C"> </a></pre></div></div><p></p><p> 我在更改变量之前使用了使用 href 的 find_element,但我认为它不起作用。</p><pre> submit_btn = driver.find_element(By.XPATH,"//a[@href='JavaScript:onclick=bookProsecc(']")</pre><p> 访问按钮元素的最佳方式是什么?</p></div> - Selenium, WebDriver : Get Element, " <a href=Javascript " with changing variables

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 硒单击链接带有Javascript的href - selenium click link href with javascript 硒webdriver单击javascript链接 - selenium webdriver click javascript link Selenium WebDriver IEDriverServer 点击链接 Javascript doPostBack - Selenium WebDriver IEDriverServer Click Link Javascript doPostBack Selenium webdriver JavaScript无法点击链接 - Selenium webdriver JavaScript not able to click on link Selenium,WebDriver:获取元素,“<div id="text_translate"><p> javascript 获取元素的按钮源码: </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> <a href="JavaScript:onclick=bookProsecc('20220512','1550','A','B','0','','')"> <img src="/image/cal_app.jpg" alt="C"> </a></pre></div></div><p></p><p> 我在更改变量之前使用了使用 href 的 find_element,但我认为它不起作用。</p><pre> submit_btn = driver.find_element(By.XPATH,"//a[@href='JavaScript:onclick=bookProsecc(']")</pre><p> 访问按钮元素的最佳方式是什么?</p></div> - Selenium, WebDriver : Get Element, " <a href=Javascript " with changing variables 硒点击javascript href - Selenium click on javascript href 如何使用Selenium Webdriver处理Javascript单击? - How to handle Javascript click by using selenium Webdriver? 如何使用 JavaScript 单击 Selenium WebDriver 中的元素? - How to click an element in Selenium WebDriver using JavaScript? 单击href链接的Javascript - Javascript to click on href link 使用Ruby WebDriver(Selenium 2.0)单击javascript链接 - Using Ruby webdriver (Selenium 2.0) to click on a javascript link
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM