Python Selenium如何单击特定文本旁边的按钮? [英] Python Selenium How to click a button next to specific text?

查看:629
本文介绍了Python Selenium如何单击特定文本旁边的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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>

在上面的代码中,我想单击删除"按钮,问题是每次添加回购协议时,回购协议的类名称,显示文本的类名称以及删除"按钮的类名称为除回购显示名称外,所有回购都相同.

In the above code I want to click the Delete button, the problem is that every time a Repo is added, the class name of the repo, the class name of the display text, and the class name of the delete button is all the same for all the repos except for the repo display name.

我要做的就是单击特定回购名称旁边的删除按钮. 我尝试了.//span[text() = "RepoName"],但这只能检测到回购名称,而不是其旁边的按钮.

All I want to do is click the delete button next to a specific repo name. I tried .//span[text() = "RepoName"] but this only detects the repo name and not the button next to it.

我对硒还很陌生,我对如何处理硒感到困惑.

I am pretty new to selenium and I am confused on how to go about it.

推荐答案

如果要在某个RepoX之后使用按钮div,请找到跨度并使用Button类获得以下div:

If you want the button div after a certain RepoX, find the span and get the following div with the Button class:

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

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

If you did not know the class name but you know it is the next div:

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

这篇关于Python Selenium如何单击特定文本旁边的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆