为什么nth-child等效于css选择器但不适用于xpath?

时间:2018-01-02 15:04:14

标签: selenium xpath

我正在尝试获取此website的此css_selector的xpath等效项。

要添加6个元素:div:nth-child(1)。对于xpath,它将是//div[1]但这没有区别。我想要左侧结果tab

下的所有6个数字

的CSS:

div:nth-child(1) > proposition-return > div > animate-odds-change > div > div

返回6个元素

xpath(非常相似):

//div[@class='propositions-wrapper'][1]//div[contains(@class, 'proposition-return')]//animate-odds-change//div//div

返回18

我希望6.

<div ng-repeat="odd in odds" class="animate-odd ng-binding ng-scope" ng-class="{
  'no-animation': oddsShouldChangeWithoutAnimation

    }" style="">2.05</div>

2 个答案:

答案 0 :(得分:0)

那些是完全不同的选择者:

CSS: div:nth-child(1) > proposition-return > div > animate-odds-change > div > div

  

查找作为其父级的第一个子级的每个div,然后获取其直接proposition-return子级(我假设您希望像.proposition-return一样使用它,然后直接{{1}等等..

XPATH: div

  

//div[@class='propositions-wrapper'][1]//div[contains(@class, 'proposition-return')]//animate-odds-change//div//div作为类查找所有div个元素,然后只获取第一个元素。之后,找到所有proposition-wrapper的{​​{1}}类,它们是前一个元素的后代,依此类推......

divproposition-return非常不同,[1]nth-child(1)不同,但//是。

为了获得你想要的特定元素,我会使用这个xpath:

>

答案 1 :(得分:0)

该网站在全球范围内无法使用,但查看您提供的图片会有所帮助。正如eLRuLL指出的那样,你的xpath不等同于css。

尝试摆脱一些双斜线

//div[@class='propositions-wrapper']//div[contains(@class, 'proposition-return')]/animate-odds-change/div/div[contains(@class, 'animate-odd')]