简体   繁体   English

使用xpath选择所有具有onclick的锚点标签

[英]select all anchor tags having onclick using xpath

I want to select all anchor tags on a web page having 'onClick' function defined as an attribute using XPATH. 我想使用XPATH在具有“ onClick”功能定义为属性的网页上选择所有锚标签。 The web page i am targeting has anchor tags like this 我定位的网页上有这样的定位标记

<a href="status.php?op=del&amp;status_id=2" onclick="return confirm('Are you sure you want to delete this item?')">Delete</a>

After a lot of searching I found following potential solutions but none of them seem to work. 经过大量搜索,我发现了以下潜在的解决方案,但似乎没有一个可行。 I have tried (and failed) 我尝试过(失败了)

//a[@onclick|@href]
//a[onclick]
//a[@onclick]
//a/@*[name()='onclick']
./*[onclick]  #this should have selected all nodes with onclick function

I also tried 我也试过

//a/@onclick

but this only returns the onclick function definition where as i want the entire anchor tag. 但这仅返回onclick函数定义,因为我需要整个锚标记。

Question: How do i get all the anchor tags that have onclick function defined as an attribute using XPATH? 问题:如何使用XPATH获取所有具有定义为属性的onclick函数的锚标记?

The XPath //a[@onclick] should work. XPath //a[@onclick]应该可以工作。

Try executing this in the console of your browser to alert each anchor: 尝试在浏览器的控制台中执行此操作,以警告每个锚点:

(function(){
  var withOnclick = document.evaluate('//a[@onclick]', document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
  var anchor = withOnclick.iterateNext();
  while (anchor) {
    alert(anchor.outerHTML);
    anchor = withOnclick.iterateNext();
  }
}())

如果您使用的是jQuery,则$("a[onclick]")应该可以找到所有带有onclick的锚点

Finally i was able to do it using 终于我能够使用

//a[@onclick]

Earlier i was applying this on incorrect response object and hence was not able to get the links. 之前我在错误的响应对象上应用了此方法,因此无法获取链接。 To add further, to get the 'url' values one could do: 要进一步添加,要获取“ url”值,可以执行以下操作:

HtmlXPathSelector(response).select('//a[@onclick]/@href').extract()

this will return the list of all the links that belong to an anchor tag having 'onclick' function defined as an attribute. 这将返回属于锚标签的所有链接的列表,该锚标签具有定义为属性的“ onclick”功能。

Thanks for your answers. 感谢您的回答。

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

相关问题 根据href选择所有锚标记并进行更改 - Select all anchor tags based on href and Change it 您如何使用 Xpath 在 div 内部 html 中查找所有锚标签“一般” - How do you use Xpath to find All anchor tags “In General” within a div Inner html 聆听锚标记中所有可能包含子代的“ onClick”事件 - Listen to all “onClick” events from anchor tags that could possibly contain children onClick事件不适用于Firefox中的定位标记 - onClick event does not work for anchor tags in Firefox 如何在 Javascript 中使用 RegEx 获取锚标签中未包含的所有字符? - How to get all the characters not contained in anchor tags using RegEx in Javascript? 使用锚标签滚动到 - Using Anchor Tags to Scroll to 使用jQuery循环遍历HTML页面的所有锚标签? - loop through all anchor tags of html page using jquery? 使用XPath选择包含特定文本的LI时遇到问题 - Having Trouble Using XPath to Select LI Containing Specific Text 如何使用jquery / javascript处理来自多个动态创建的定位标记的onclick事件? - How to handle onclick events from multiple dynamically created anchor tags using jquery/javascript? 复选框 select 使用 jquery 在锚链接上全部/取消选择全部 - Checkbox select all/ unselect all on anchor link using jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM