如何在 python 中使用 Selenium 包单击多个复选框和下拉列表?

佩德罗·BP

我正在尝试选择此网站中未选中的复选框:https : //dataunodc.un.org/GSH_app,在“国家数据”中。此外,我想从下拉菜单(“显示...条目”)中选择“100”。

我正在使用硒和蟒蛇。你能告诉我怎么做吗?

这是与复选框选择对应的 HTML 代码的一部分:

                    <label class="checkbox-inline">
                      <input type="checkbox" name="YearVar" value="1990" checked="checked">
                      <span>1990</span>
                    </label>
                    <label class="checkbox-inline">
                      <input type="checkbox" name="YearVar" value="1991">
                      <span>1991</span>
                    </label>
                    <label class="checkbox-inline">
                      <input type="checkbox" name="YearVar" value="1992"

而这个对应下拉菜单:

 name="DataTables_Table_0_length" aria-
controls="DataTables_Table_0" class=""><option
 value="10">10</option><option
 value="25">25</option><option
 value="50">50</option><option 
 value="100">100</option></select> entries</label>
昆杜克

要获取所有未选中的复选框,然后单击这些复选框。

Induce WebDriverWait() 和visibility_of_all_elements_located() 以及以下XPATH选项。然后迭代元素并单击每个元素。

#Get all checkbox which are not selected.
allchekbox=WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.XPATH,"//input[@name='YearVar' and not(@checked='checked')]")))

for item in allchekbox:
    item.click()

为了选择dropdown使用 selenium 的值,请选择class

InduceWebDriverWait()visibility_of_element_located() 和任一Xpath选项。

XPATH 1:

 # Select Item from dropdown
element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//select[starts-with(@name,'DataTables_Table_')]")))
select = Select(element)
select.select_by_value("100")

要么

XPATH 2:

element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//label[normalize-space(text())='Show']/select")))
select = Select(element)
select.select_by_value("100")

这是完整的代码:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
import time
driver = webdriver.Chrome()
driver.get("https://dataunodc.un.org/GSH_app")
driver.maximize_window()

#Switch the iframe in order to access the link
WebDriverWait(driver,15).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@src='https://unodc.shinyapps.io/GSH_App/']")))
#Click on National Data link
WebDriverWait(driver,5).until(EC.element_to_be_clickable((By.XPATH,'//ul[@class="nav navbar-nav"]//a[text()="National Data"]'))).click()

#Get all checkbox which are not selected.
allchekbox=WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.XPATH,"//input[@name='YearVar' and not(@checked='checked')]")))

#iterate and click each checkbox
for item in allchekbox:
    item.click()

#To avoid StaleElementReferenceException add time.sleep()
time.sleep(2)
# Select Item from dropdown
element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//select[starts-with(@name,'DataTables_Table_')]")))
select = Select(element)
select.select_by_value("100")

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Python中使用Selenium选中复选框

如何在 python 中使用 selenium 选择下拉列表的项目

如何在 Python 中使用 Selenium WebDriver 根据 html 选择复选框

如何在Selenium中使用python检查Chrome中的复选框

如何在 Python3 中使用 selenium 检查复选框

使用 Python Selenium 单击复选框

如何在 Python 中使用 Selenium 单击下拉列表的 li 元素^

如何在 python 中使用 Selenium 在 ul 中单击 li?

如何在python中使用Selenium执行右键单击?

如何在Python中使用Selenium从站点下载多个文件以选择下拉列表的每个选项

如何在python中使用Selenium和Beautifulsoup解析网站?

如何在python中使用Selenium和Beautifulsoup解析网站?

如何在Selenium和Python中使用类型查找元素

如何在 Python 中使用 selenium 刮取 > 和 < 之间的值?

如何在Python中使用Selenium?

如何在Selenium Python中使用JavaScript

如何通过Selenium Python按照HTML单击复选框

如何在 selenium 和 python 中使用下拉菜单和输入文本框?

如何在python3中使用selenium在javascrip站点中选择下拉列表?

如何在输入类型的python中使用webdriver selenium选择下拉列表元素?

如何通过Selenium和Python提供的HTML单击基于角度的复选框?

如何在python和selenium中使用execute_script从下拉列表中选择一个值

使用Selenium和Python选中复选框

在 Python Jupyter Notebook 中使用 Selenium 单击下拉列表

如何在 python 中使用 selenium 在不和谐页面上单击此元素?

如何在Selenium Webdriver中使用Python单击:: before / :: after CC标签?

如何在 python 中使用 Selenium 在 Myntra 上单击“显示更多”

如何在Python中使用Selenium抓取动态生成的多个div

如何在python中使用Selenium和javascript跟踪鼠标事件和位置?