如何在动态表Selenium webdriver中选择下拉列表

eanckbw9  于 2023-02-04  发布在  其他
关注(0)|答案(4)|浏览(149)

我有HTML表格,如

<table id='table1'>
<tbody>
    <tr>
        <td colspan='2'>Food</td>
    </tr>
    <tr>
        <td>Burger</td>
        <td>
            <select class="form-control input-sm" id="xx2" onchange="count(this)">
                <option value="1">Burger</option>
                <option value="2">spaghetti</option>
                <option value="3">Kebab</option>
            </select>
        </td>
    </tr>       
    <tr>
        <td colspan='2'>Drink</td>
    </tr>
    <tr>
        <td>Burger</td>
        <td>
            <select class="form-control input-sm" id="kj2" onchange="count(this)">
                <option value="1">Coffe</option>
                <option value="2">Tea</option>
                <option value="3">Milk</option>
            </select>
        </td>
    </tr>
    <tr>
        <td colspan='2'> ... </td>
    </tr>
    <tr>
        <td> .......... </td>
        <td>
            <select class="form-control input-sm" id="jj" onchange="count(this)">
                <option value="1"> ... </option>
                <option value="2"> ..... </option>
                <option value="3"> .... </option>
            </select>
        </td>
    </tr>       
</tbody>

在Selenium Web驱动程序中,如何在动态表的下拉列表中选择第一个值(总行数为varry,列跨度为colspan)
我有get表,我想获取全部并填充表中的dropdownlist

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="table1"]/tbody')))    
table = driver.find_element(By.XPATH, '//*[@id="table1"]/tbody')
pxiryf3j

pxiryf3j1#

获取所有不同的数据

from bs4 import BeautifulSoup
import requests
  
main_site = requests.get("http://yourhtmlfile.html")
soup = BeautifulSoup(main_site.content, 'html.parser')
table = soup.find(attrs={'id':'table1'})
select = table.find(attrs={'id':'xx2'})
res = []
for option in select.find_all('option'):
    res.append(option["value"])

所有选项数据都存储在res列表中

selectedDropDownList = Select(driver.find_element(By.ID, "xx2"))
selectedDropDownList.select_by_visible_text(res[0])

因此,您可以重复此方法来填充所有下拉列表

7cwmlq89

7cwmlq892#

您可以使用以下XPath:
要选择第一个下拉菜单:

dropdown_1 = Select(driver.find_element(By.XPATH, "(.//*[@id='table1']//select[@class='form-control input-sm'])[1]"))

要选择第二个或第三个下拉列表,您可以更改XPath中的索引:

(.//*[@id='table1']//select[@class='form-control input-sm'])[2]

(.//*[@id='table1']//select[@class='form-control input-sm'])[3]

要从下拉列表中选择第一个选项:

dropdown_1.select_by_value(1)

dropdown_1.select_by_index(0)
nkoocmlb

nkoocmlb3#

根据你的html结构,你可以使用fooddrink这样的项目引用,然后将select元素作为目标。
使用下面的xpath标识每个下拉列表元素。

下降1:"//td[text()='Food']/following::select[1]"
下降2:"//td[text()='Drink']/following::select[1]"

使用selenium select类来选择下拉列表值。要处理动态元素,请使用WebDriverWait()并希望元素可单击。

selectFood=Select(WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//td[text()='Food']/following::select[1]"))))

selectFood.select_by_value(1)

selectDrink=Select(WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//td[text()='Drink']/following::select[1]"))))

selectDrink.select_by_value(1)

您需要导入以下库。

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
vohkndzv

vohkndzv4#

在提供的HTML中总共有三(3)个html-select元素,要选择一个选项,您必须使用Select()类,并将WebDriverWait归纳为element_to_be_clickable(),您可以使用以下locator strategies之一:

# using select_by_visible_text() to select spaghetti
Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='xx2']")))).select_by_visible_text("spaghetti")

# using select_by_index() to select Tea
Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='kj2']")))).select_by_index(2)

# using select_by_value() to select the second option from the third dropdown
Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='jj']")))).select_by_value('2')

相关问题