Home > Software engineering >  How to select dropdown in the dynamic table Selenium webdriver
How to select dropdown in the dynamic table Selenium webdriver

Time:02-02

I have HTML tables like

<table id='table1'>
<tbody>
    <tr>
        <td colspan='2'>Food</td>
    </tr>
    <tr>
        <td>Burger</td>
        <td>
            <select  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  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  id="jj" onchange="count(this)">
                <option value="1"> ... </option>
                <option value="2"> ..... </option>
                <option value="3"> .... </option>
            </select>
        </td>
    </tr>       
</tbody>

In selenium web driver, how to select first value at dropdownlist in the dynamic table (total rows is varry and there is colspan)

I have get table, and I want to get all and fill dropdownlist in the table

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

CodePudding user response:

to get all vary data

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"])

all option data is stored into res list

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

so, you can repeat this method to fullfill all dropdownlist

CodePudding user response:

You can use the below XPath:

To select the first dropdown:

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

To select the second or third dropdown you can change the index in the XPath:

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

or

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

To select the first option from the dropdown:

dropdown_1.select_by_value(1)

or

dropdown_1.select_by_index(0)

CodePudding user response:

Based on your html structure, you can use item reference like food or drink and then target following select element.

Use following xpath to identify each dropdown element.

dropdwon 1: "//td[text()='Food']/following::select[1]"

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

Use selenium select class to select the drop down values. To handle dynamic element use WebDriverWait() and want for element clickable.

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)

You need to import following libraries.

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
  • Related