使用python从selenium弹出菜单中获取项目列表

2024-10-01 07:20:02 发布

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

如何使用SeleniumPythonAPI从弹出窗口中的以下块中提取列表['Cat1','Cat2',Cat3']。在

...
<div class="categoryPopup" id="categoryPopup">
<div class="categoriesList"><li id="addCategory_231329891" class="edit_form_add_category">
            <input id="checkCategory_231329891" class="fl" type="checkbox"><label for="checkCategory_231329891"><span class="title fl">Cat1</span></label>
            <div class="cb"></div>
        </li><div class="cb"></div><li id="addCategory_231329901" class="edit_form_add_category">
            <input id="checkCategory_231329901" class="fl" type="checkbox"><label for="checkCategory_231329901"><span class="title fl">Cat2</span></label>
            <div class="cb"></div>
        </li><div class="cb"></div><li id="addCategory_231330011" class="edit_form_add_category">
            <input id="checkCategory_231330011" class="fl" type="checkbox"><label for="checkCategory_231330011"><span class="title fl">Cat3</span></label>
            <div class="cb"></div>
</div>
...

尝试使用以下代码无效:

^{pr2}$

Tags: divformaddidinputlieditlabel
1条回答
网友
1楼 · 发布于 2024-10-01 07:20:02

尝试使用cssSelector,如下所示。我假设你想选中所有的复选框。以下代码将返回输入列表。您还应该使用_elements_来查找元素的集合。请参阅api文档。在

from selenium import webdriver
browser = webdriver.Firefox()
browser.get("http://targeURL")
# series of successful browser.get_element_by...
browser.find_elements_by_css_selector("#categoryPopup input")

相关问题 更多 >