有关在Selenium中找到以下元素的任何建议

时间:2015-02-23 17:08:22

标签: selenium xpath selenium-webdriver element locate

下面的示例HTML。我想找到包含文本 Person Attributes和 Age 的元素。

<div id="ext-gen210" class="x-tool x-tool-toggle"></div>
<span id="ext-gen214" class="x-panel-header-text">Person Attributes</span>
</div>
<div id="ext-comp-1071" class=" DDView ListView" style="height: auto;">
   <p class="dragItem "><i class="icon-Gen"></i>Age</p>
</div>

注意:我正在寻找一种不使用xpathidclassName的解决方案,因为它们可能会随着我网站的每个新版本而改变。

我尝试使用

找到它们
  

'name' - &gt; By.name(“Person Attributes”)和By.name(“Age”)但都失败了。

1 个答案:

答案 0 :(得分:2)

By.name会检查 name属性。您需要的是使用By.xpath检查元素的文字

By.xpath('//div[span/text() = "Person Attributes"]')

或者,您还可以检查id元素是否以 ext-gen开头:

By.xpath('//div[starts-with(@id, "ext-gen")]')
相关问题