用于私有列表的Xpath <webelement>用于follow-sibling的corect定义

时间:2018-01-27 16:57:15

标签: jquery css xpath intellij-idea

我正在使用follow-sibling为webelement创建一个私有列表的Xpath。我要创建一个包含主页产品的列表:http://automationpractice.com/index.php

使用以下Xpath,它不会跟随产品列表: xpath =&#34; // ul [contains(@class,&#39; product_list grid row homefeatured&#39;)] / following-sibling :: li&#34;

你能帮忙请为列表webelemet

创建corect xpath

css脚本:

<ul id="home-page-tabs" class="nav nav-tabs clearfix">

  

enter image description here

2 个答案:

答案 0 :(得分:0)

我认为对于给定的网址,包含product_list grid row homefeatured的class属性没有following-siblings li。从那时起,下面的兄弟将是下一个ul

也许您正在寻找:

//ul[contains(@class, 'product_list grid row homefeatured')]/li

要获取ul&#39;中的所有列表项,您可以使用例如:

//ul[contains(@class, 'product_list grid row')]/li

答案 1 :(得分:0)

据我了解,您使用<ul id="home-page-tabs" class="nav nav-tabs clearfix">来查看图片中显示的li元素。为此,您的xpath应该看起来像

.//ul[@id='home-page-tabs' and @class='nav nav-tabs clearfix']/following-sibling::div[@class='tab-content']/ul[@id='homefeatured' and @class='product_list grid row homefeatured tab-pane active']/li

可能在您的情况下不起作用的是,在您的xpath中,ul"id="home-page-tabs"li"@id='homefeatured"不在同一层次结构中,即他们有不同的父母。因此,您无法使用following-sibling获取li。您可以做的是转到div following-sibling ul,然后转到li div的孩子。string x = "1x"; string y = "2b"; char startCharacter = x.Substring(x.Length-1)[0]; char endCharacter = y.Substring(y.Length-1)[0]; int startNumber = int.Parse(x.Substring(0, x.Length - 1)); int endNumber = int.Parse(y.Substring(0, y.Length - 1)); var range = new List<string>(); string alphabet = "abcdefghijklmnopqrstuvwxyz"; for(int i = startNumber; i <= endNumber; ++i) { int currentCharEnd = (i == endNumber) ? alphabet.IndexOf(endCharacter) : alphabet.Length - 1; for(int j = alphabet.IndexOf(startCharacter); j <= currentCharEnd; ++j) { range.Add(i.ToString() + alphabet[j]); } startCharacter = 'a'; } // range is now { "1x", "1y", "1z", "2a", "2b" }

希望这能解释并提供帮助。