Selenium - 如何仅在特定div内搜索元素?
问题描述:
我在许多地方的许多页面上使用了我的代码中的一个元素。具体来说,它是一个日历元素。该元素包含很多部分,但是,无论使用何地,这些内部部分总是相同的。我想要做的是搜索日历元素内部的东西,而不管日历元素在哪里。Selenium - 如何仅在特定div内搜索元素?
就像一个简化的情况下,考虑以下因素:现在
<div class="not-calendar">
<a href="/not-calendar-link">A Link</a>
</div>
<div class="calendar">
<a href="/calendar-link">A Link</a>
</div>
,我愿做线沿线的东西:
$this->click("//a[text()='A Link']");
但是,这是不明确的。我想为A Link文本做相同类型的“搜索”,但只能在类别日历的div内搜索。请记住,我不希望直接XPath到日历div,因为我期望它在许多页面的不同位置显示。我该如何去做这件事?非常感谢!
答
Xpath是相当强大的,你有没有试过检查文档?
//bbb Selects all bbb elements no matter where they are in the document
aaa/bbb Selects all bbb elements that are children of aaa
aaa//bbb Selects all bbb elements that are descendant of the aaa element, no matter where they are under the aaa element
你要找的表达://div[@class='calendar']/a
哦,我明白了。我想我对XPath还不够了解。太棒了,谢谢! – golmschenk
我要中断并说CSS是优越的,因为xpath在IE上有主要的性能问题。尝试:http://sauceio.com/index.php/2010/01/selenium-totw-css-selectors-in-selenium-demystified/ – Greg
我也更喜欢CSS,因为我习惯了jQuery选择器。 –