0

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 class="form-control input-sm" 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" class="btn btn-primary" id="submit"><span class="fa fa-save"></span> Save</button>

3 Answers 3

1

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()
Sign up to request clarification or add additional context in comments.

2 Comments

how to make sure column ::td/select is exist? because some row is merging/span column and show error message => TypeError: can only concatenate str (not "NoneType") to str
If it doesn't exist, the first line will throw a NoSuchElementException. There's nothing in my code that is concatenating strings so I'm not sure what you are referring to. You probably need to do some investigating and if you can't figure it out, create a new question with the code sample, error message, etc. and we'll help you.
1

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.