Home > Blockchain >  Python Selenium webdriver get XPATH and select dropdown
Python Selenium webdriver get XPATH and select dropdown

Time:01-28

I've found the word 'Burger' in HTML table with this code

findRow = driver.find_element(By.XPATH, "//*[contains(text(),'Burger')]").value_of_css_property('#name')
  1. how do I can get XPATH 'Burger'?
  2. how do I can select the column beside it (example select 'Fish' beside column 'Burger') and then submit button?

HTML code

    <tbody>
        <tr>
            <td>..</td>
            <td>.....</td>
        </tr>
        <tr>
            <td>Burger</td>
            <td>
                <select  id="sel222" name="sel222" type="68" group="433" onchange="count(this)">
                    <option value="1">Vegetables</option>
                    <option value="2">Fish</option>
                    <option value="3">Beef</option>
                </select>
            </td>
        </tr>       
    </tbody>
</table>
              <button type="button"  id="submit"><span ></span> Save</button>

CodePudding user response:

In this scenario, you can identify the select list box using xpath following technique. Use the below xpath to identify select object

//td[contains(.,'Burger')]/following::select

CodePudding user response:

You can select the option Burger using the below XPath:

.//td[text()='Burger']

You can select the options inside select tag using the below XPath:

.//td[text()='Burger']//parent::tr//select/option

CodePudding user response:

Based on the HTML posted, the simplest XPath to find "Burger" would be,

//td[text()='Burger']

The XPath to find the SELECT in the cell to the right of the "Burger" cell would be,

//td[text()='Burger']/following-sibling::td/select
  ^ the XPath to find the TD that contains "Burger", from above
                      ^ then find the sibling TD
                                            ^ then the SELECT child of the sibling

Putting this all together to select "Fish" from the SELECT element next to the "Burger" cell and click Submit,

from selenium.webdriver.support.ui import Select

select = Select(driver.find_element(By.XPATH, "//td[text()='Burger']/following-sibling::td/select"))
select.select_by_visible_text("Fish")
driver.find_element(By.ID, "submit").click()
  • Related