Python Selenium如何单击特定文本旁边的按钮?

时间:2016-03-23 18:46:07

标签: python selenium selenium-webdriver

<div class = "Repo List">
    <div class = "Repo">
        <div class = "Text">
            <span class = "Display Text">Repo1</span>
        </div>
        <div class = "Button">
            <div class = "Click Button">Delete</div>
        </div>
    </div>

    <div class = "Repo">
        <div class = "Text">
            <span class = "Display Text">Repo2</span>
        </div>
        <div class = "Button">
            <div class = "Click Button">Delete</div>
        </div>
    </div>

    <div class = "Repo">
        <div class = "Text">
            <span class = "Display Text">Repo3</span>
        </div>
        <div class = "Button">
            <div class = "Click Button">Delete</div>
        </div>
    </div>
</div>

在上面的代码中我想点击删除按钮,问题是每次添加Repo时,repo的类名,显示文本的类名和删除按钮的类名是除了回购显示名称之外,所有回购都是一样的。

我想要做的就是点击特定回购名称旁边的删除按钮。 我试过.//span[text() = "RepoName"],但这只检测回购名称,而不是旁边的按钮。

我对硒很新,我很困惑如何去做。

2 个答案:

答案 0 :(得分:1)

如果您想在某个RepoX之后使用按钮div,找到span并使用Button类获得以下div:

pr

如果您不知道班级名称,但您知道它是下一个div:

.xpath("//span[text()='Repo3']/following::div[@class='Button']")

答案 1 :(得分:0)

点击&#34;删除&#34; &#34; Repo1&#34;之后的按钮文本:

driver.find_element_by_xpath("//span[.='Repo1']/following::div[.='Delete'][1]").click()