使用 Selenium WebDriver 选择元素后跟文本 [英] Selecting Element followed by text with Selenium WebDriver

查看:24
本文介绍了使用 Selenium WebDriver 选择元素后跟文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Selenium WebDriver 和 Python 绑定来自动化一些单调的 WordPress 任务,到目前为止它一直非常简单.我试图选择一个复选框,但我可以识别它的唯一方法是通过它后面的文本.这是 HTML 的相关部分:

I am using Selenium WebDriver and the Python bindings to automate some monotonous WordPress tasks, and it has been pretty straightforward up until this point. I am trying to select a checkbox, but the only way that I can identify it is by the text following it. Here is the relevant portion of HTML:

<li id="product_cat-52">
    <label class="selectit">
       <input value="52" type="checkbox" name="tax_input[product_cat][]" id="in-product_cat-52"> polishpottery
    </label>
</li>

我的脚本中用于标识此复选框的唯一信息是字符串polishpottery".有没有办法只知道后面的文本来选择那个复选框?

The only information that I have in my script to identify this checkbox is the string "polishpottery". Is there any way to select that checkbox knowing only the text that follows?

推荐答案

正如@sherwin-wu 已经说过的,您应该找到一种方法来根据 id 或名称或类(很可能是它们的组合)来选择您想要的内容).在您的示例中,似乎有足够的可能性可以这样做,尽管我不知道页面的其余部分通常是什么样子.

As @sherwin-wu already said, you should find a way to select what you want based on id or name or class (and most likely a combination of it). In your example there seem to be enough possibilities to do so, although I don't know what the rest of the page normally looks like.

话虽如此,可以使用 XPath 选择器来执行您要求的操作

Having that said, it's possible to do what you asked for using an XPath selector like

driver.find_element_by_xpath("//li/label/input[contains(..,'polishpottery')]")

这篇关于使用 Selenium WebDriver 选择元素后跟文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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