[英]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&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.