小编Dom*_*kin的帖子

Selenium Webdriver在子元素中查找元素

我试图用Selenium(版本2.28.0)搜索子元素中的元素,但selenium des似乎没有将其搜索限制为子元素.我这样做错了还是有办法使用element.find来搜索子元素?

举个例子,我用这段代码创建了一个简单的测试网页:

<!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>
Run Code Online (Sandbox Code Playgroud)

我的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 …
Run Code Online (Sandbox Code Playgroud)

python selenium xpath

52
推荐指数
3
解决办法
5万
查看次数

标签 统计

python ×1

selenium ×1

xpath ×1