python selenium checkbox n元素if tex

2024-04-19 17:17:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是pythonselenium,需要访问标签为“thisis2ndID”的元素并启用复选框?在

<h3 class="activator"> … </h3>
<div class="accordion-panel">
    <ul>
        <li class="field">
            <input id="thisis1stID" type="checkbox" name=""></input>
            <label for="thisis1stID"> … </label>
        </li>
        <li class="field">
            <input id="thisis2ndID" type="checkbox" name=""></input>
            <label for="thisis2ndID"> … </label>
        </li>
        <li class="field">
            <input id="thisis3rdID" type="checkbox" name=""></input>
            <label for="thisis3rdID"> … </label>
        </li

尝试过这些,但似乎不起作用。。。在

^{pr2}$

有什么想法吗?在


Tags: nameidfieldforinputtypelilabel
3条回答

我用了几种不同的方法来完成这项工作,但是在您的例子中,最好的方法是迭代所有的li标记并搜索您要查找的id值,试试这个

browser.find_element_by_xpath("//div[@class='accordion-panel']")
for i in browser.find_elements_by_tag_name("li"):
    ...

如果在python中有使用cssSelector查找元素的规定,那么下面的选择器和click()事件将启用复选框。。。。在

"div.accordion-panel li.field+li.field>#thisis2ndID"

我只研究过java,所以不能用python提供精确的代码。。在

you can click here to know more about different kind of selectors

这起作用了…很抱歉,需要点击激活器来加载子选项。。。在

browser.find_element_by_class_name('activator').click()
browser.find_element_by_xpath("//div[@class='accordion-panel']")
browser.find_element_by_id('thisis2ndID').click()
time.sleep(5)

相关问题 更多 >