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"> text-to-copy </div> <div class="cell"> <input type="text" size="10" id="known_id" onchange="update(this.id);" onclick="setElementId(this.id);"/> X </div> <div class="cell right"> <div id="some_id">?</div> </div> </div>
It looks something like this:
From this table I would like to get the
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 (
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 (
I believe you can fairly use
xpath, all other
would not work, becasue we have to traverse upward in DOM.
The below xpath is dependent on
You have to either use
.get_attribute etc to get the text.
Sample code :
time.sleep(5) element = selenium_driver.find_element_by_xpath("//input[contains(@id,'known_id')]/../preceding-sibling::div").get_attribute('innerText') print(element)