使用Selenium WebDriver选择Element后跟文本

And*_*rew 6 python selenium webdriver selenium-chromedriver

我正在使用Selenium WebDriver和Python绑定来自动执行一些单调的WordPress任务,直到此时它才非常简单.我正在尝试选择一个复选框,但我能识别它的唯一方法是通过它后面的文本.以下是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>
Run Code Online (Sandbox Code Playgroud)

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

zpe*_*pea 8

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

说到这一点,你可以按照你想要的那样使用XPath选择器

driver.find_element_by_xpath("//li/label/input[contains(..,'polishpottery')]")
Run Code Online (Sandbox Code Playgroud)