让 Selenium 选择特定的下拉菜单 PYTHON3
Make Selenium choose specific Dropdown menu PYTHON3
我需要 Selenium 来选择具有从抓取中获得的 VALUE 的菜单。
这是下拉菜单的 HTML 代码部分:
<select class="graySelect" name="sch_bub_nm" id="sch_bub_nm"
title="Case Number" onchange="onChangeBub();">
<option value="000100">Case1</option>
<option value="000200">Case2</option>
<option value="000201">Case3</option>
.
.
.
这是我到目前为止编写的代码:
def MenuChoose():
driver.find_element_by_css_selector('#sch_bub_nm').click()
driver.find_element_by_xpath("//*[@id="sch_bub_nm"]/option[1]")
如您所见,我尝试选择菜单,但卡住了,因为 xpath
没有显示我可以将代码定向到的值。
您应该使用 Select
来获取下拉值。我给了 3 个选项 select 值。
from selenium.webdriver.support.select import Select
select=Select(driver.find_element_by_id("sch_bub_nm"))
select.select_by_index(1) #select index value
select.select_by_visible_text("Case2") # select visible text
select.select_by_value("000201") # Select option value
让我知道这是否可行。
您需要创建一个 select 元素才能与之交互。
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_css_selector('#sch_bub_nm'))
select.select_by_index(1) # Choose the position you want
查看 selenium-python 文档以查看 select 的更多选项。
我需要 Selenium 来选择具有从抓取中获得的 VALUE 的菜单。 这是下拉菜单的 HTML 代码部分:
<select class="graySelect" name="sch_bub_nm" id="sch_bub_nm"
title="Case Number" onchange="onChangeBub();">
<option value="000100">Case1</option>
<option value="000200">Case2</option>
<option value="000201">Case3</option>
.
.
.
这是我到目前为止编写的代码:
def MenuChoose():
driver.find_element_by_css_selector('#sch_bub_nm').click()
driver.find_element_by_xpath("//*[@id="sch_bub_nm"]/option[1]")
如您所见,我尝试选择菜单,但卡住了,因为 xpath
没有显示我可以将代码定向到的值。
您应该使用 Select
来获取下拉值。我给了 3 个选项 select 值。
from selenium.webdriver.support.select import Select
select=Select(driver.find_element_by_id("sch_bub_nm"))
select.select_by_index(1) #select index value
select.select_by_visible_text("Case2") # select visible text
select.select_by_value("000201") # Select option value
让我知道这是否可行。
您需要创建一个 select 元素才能与之交互。
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_css_selector('#sch_bub_nm'))
select.select_by_index(1) # Choose the position you want
查看 selenium-python 文档以查看 select 的更多选项。