Python, Selenium: How to get text next to element

I’m fairly new to selenium and I’m trying to get the text of a cell next to a known element.

This is an excerpt of a webtable:

<div class="row">
    <div class="cell">
    <div class="cell">
        <input type="text" size="10" id="known_id" onchange="update(;" onclick="setElementId(;"/>
    <div class="cell right">
        <div id="some_id">?</div>

It looks something like this:

enter image description here

From this table I would like to get the text-to-copy with selenium. As the composition of the table can vary, there is no way to know that cells xpath. Therefore I can not use selenium_driver.find_element_by_xpath(). The only known thing is the id of the cell next to it (id=known_id).

The following pseudo code is to illustrate what I’m looking for:

element = selenium_driver.find_element_by_id("known_id")
result = element.get_visible_text_from_cell_before_element()

Is there a way to get the visible text (text-to-copy) with selenium?


I believe you can fairly use xpath, all other locators that Selenium supports would not work, becasue we have to traverse upward in DOM.

The below xpath is dependent on known_id


You have to either use .text or .get_attribute etc to get the text.

Sample code :

element = selenium_driver.find_element_by_xpath("//input[contains(@id,'known_id')]/../preceding-sibling::div").get_attribute('innerText')