我正在尝试使用Selenium(版本2.28.0)在子元素中搜索元素,但是selenium des似乎并不将其搜索限制在子元素中。 我做错了吗,还是有一种方法可以使用element.find搜索子元素?
例如,我使用以下代码创建了一个简单的测试网页:
My First Heading
My first paragraph.
My Second Heading
My second paragraph.
My Third Heading
My third paragraph.
我的python(2.6版)代码如下所示:
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."
如果我运行:
print element2.get_attribute('innerHTML')
它从第二部分返回html。 因此,硒的搜索范围并不限于element2。
我希望能够找到element2的一个子元素。 这篇文章表明我的代码应该可以使用Selenium WebDriver访问子元素,但是他的问题是由超时问题引起的。
谁能帮我了解这里发生了什么?