<p>我正在尝试使用Selenium(版本2.28.0)在子元素中搜索元素,但是Selenium des似乎没有将其搜索限制在子元素上。是我做错了还是有办法使用element.find来搜索子元素?</p>
<p>例如,我使用以下代码创建了一个简单的测试网页:</p>
<pre><code><!DOCTYPE html>
<html>
<body>
<div class=div title=div1>
<h1>My First Heading</h1>
<p class='test'>My first paragraph.</p>
</div>
<div class=div title=div2>
<h1>My Second Heading</h1>
<p class='test'>My second paragraph.</p>
</div>
<div class=div title=div3>
<h1>My Third Heading</h1>
<p class='test'>My third paragraph.</p>
</div>
</body>
</html>
</code></pre>
<p>我的python(2.6版)代码如下:</p>
<pre><code>from selenium import webdriver
driver = webdriver.Firefox()
# Open the test page with this instance of Firefox
# element2 gets the second division as a web element
element2 = driver.find_element_by_xpath("//div[@title='div2']")
# Search second division for a paragraph with a class of 'test' and print the content
print element2.find_element_by_xpath("//p[@class='test']").text
# expected output: "My second paragraph."
# actual output: "My first paragraph."
</code></pre>
<p>如果我跑:</p>
<pre><code>print element2.get_attribute('innerHTML')
</code></pre>
<p>它从第二个分区返回html。所以硒并没有限制它对元素2的搜索。</p>
<p>我希望能够找到element2的子元素。这篇文章建议我的代码应该可以工作,但是他的问题是由超时问题引起的。</p>
<p>有谁能帮我了解这里发生了什么事吗?</p>