简体   繁体   English

查找具有特定类名的元素

[英]Find elements with specific class name

for chrome, I install ChroPath to find elements on the page. 对于chrome,我安装了ChroPath在页面上查找元素。

I want to find XPath for like elements on Instagram Page, but seems that not work : 我想在Instagram页面上为类似元素找到XPath,但似乎不起作用:

//span[contains(@class,'glyphsSpriteHeart__outline__24__grey_9 u-__7')] 

also, I try it : 另外,我尝试:

/html[1]/body[1]/div[3]/div[1]/div[2]/div[1]/article[1]/div[2]/section[1]/span[1]/button[1]/span[1]

when selenium click : 当硒点击:

elenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"div._2dDPU.vCf6V div.zZYga div.PdwC2._6oveC article.M9sTE.L_LMM.JyscU div.eo2As section.ltpMr.Slqrh span.fr66n button.coreSpriteHeartOpen.oF4XW.dCJp8 > span.glyphsSpriteHeart__outline__24__grey_9.u-__7"}

how can I find XPath? 如何找到XPath? any good extension or something? 有什么好的扩展名吗?

This might help: https://selectorgadget.com/ 这可能会有所帮助: https : //selectorgadget.com/

This as well, to understand what you are manipulating: https://www.w3schools.com/xml/xpath_syntax.asp 同样,这也是为了了解您要操作的内容: https : //www.w3schools.com/xml/xpath_syntax.asp

As for your example where you go down the tree using index numbers (ie: /html[1]/body[1]), A slight change in the site will make your script to fail. 对于您使用索引编号(即:/ html [1] / body [1])从树上掉下来的示例,站点中的微小更改将使脚本失败。 Find a way to build something more robust! 找到一种方法来构建更强大的功能! Also have a look at CSS selectors if you object's appearance is known in advance. 如果您预先知道对象的外观,还可以查看CSS选择器。

To get all like buttons on instagram use css selector below: 要在instagram上获得所有类似的按钮,请使用以下CSS选择器:

span[aria-label="Like"]

You can get some helpful details here: https://www.w3schools.com/cssref/css_selectors.asp 您可以在此处获得一些有用的详细信息: https : //www.w3schools.com/cssref/css_selectors.asp

how can I find XPath? 如何找到XPath? any good extension or something? 有什么好的扩展名吗?

You cannot "find" the Xpath of an element. 您无法“找到”元素的Xpath。 There are many, many XPath's that will find any element. 有很多可以找到任何元素的XPath。 Some will be stable, others will be unstable. 有些会稳定,有些会不稳定。 The decision on which Xpath to use is based upon your understanding and experience of Selenium, and you understanding of how the Application Under Test is written and behaves. 根据您对Selenium的了解和经验,以及您对被测应用程序的编写和行为方式的了解,来决定要使用哪个Xpath。

If you are looking for a tool to experiment with different XPaths, then Chrome's built-in Developer Tools Console allows you to test both Xpath & CSS Selectors; 如果您正在寻找一种可以尝试不同XPath的工具,那么Chrome的内置开发人员工具控制台可让您同时测试Xpath和CSS选择器; 在此处输入图片说明

In your specific scenario about finding elements by class name, then CSS Selector is a much better choice than XPath as CSS selectors will treat multiple classes as an array where as XPath sees "class" as a literal string, hence why you needed to use "contains". 在按类名查找元素的特定方案中,CSS选择器比XPath更好,因为CSS选择器会将多个类视为数组,因为XPath将“类”视为文字字符串,因此,为什么需要使用“包含”。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM