Sam
Sam

Reputation: 227

Search span tag within a table class using Selenium in Python

I am trying to automate a process to log into a site and go through searching and adding an attribute. I have got it to do this but now need it to find the ID in a scroll container. Trying now find the ID that was added and click it within the window. The problem I have found in the methods I know is that it needs to be visible and with how the WebUI is built with an embedded scroll container it can't find it.

So I have been looking at the HTML and think can get to it through this way, but just need a little guidance as I haven't found anything on the internet in my search that quite meets my need.

In the sample HTML you will see where the data I need to search is within the tr tags and td class="readonly-cell cell-selected cell-selected-primary sheet-coll.

I am trying to grab the ID GPC_material which is in the <span> within the td tags.

I have been working with WebDriver to see if I could search for the ID but ran into trouble trying to trouble shoot to have the process scroll down the scroll container to find the ID.

HTML:

<table class="sheet-table horizontal-sheet-table last-row-horizontal-content-page">
 <tbody>
  <tr class="odd" style="height:32px;">
   <td class="readonly-cell cell-selected cell-selected-primary sheet-coll" data-col="0" data-row="133" style="display: table-cell; width:100px;" title="">
    <div class="sheet-cell" style="max-height:28px">
     <span>
      GPC_material
     </span>
    </div>
   </td>
   <td class=" sheet-coll" data-col="1" data-row="133" style="display: table-cell; width:100px;" title="">
    <div class="sheet-cell" style="max-height:28px">
    </div>
   </td>
   <td class=" sheet-coll" data-col="2" data-row="133" style="display: table-cell; width:100px;" title="">
    <div class="sheet-cell" style="max-height:28px">
    </div>
   </td>
   <td class=" sheet-coll" data-col="3" data-row="133" style="display: table-cell; width:100px;" title="">
    <div class="sheet-cell" style="max-height:28px">
     <span class="gwt-CheckBox">
      <input id="gwt-uid-912x133x3x604577791" tabindex="-1" type="checkbox"/>
      <label for="gwt-uid-912x133x3x604577791">
      </label>
     </span>
    </div>
   </td>
   <td class=" sheet-coll" data-col="4" data-row="133" style="display: table-cell; width:100px;" title="">
    <div class="sheet-cell" style="max-height:28px">
    </div>
   </td>
   <td class=" sheet-coll" data-col="5" data-row="133" style="display: table-cell; width:100px;" title="">
    <div class="sheet-cell" style="max-height:28px">
    </div>
   </td>
   <td class="readonly-cell sheet-coll" data-col="6" data-row="133" style="display: table-cell; width:100px;" title="">
    <div class="sheet-cell" style="max-height:28px">
    </div>
   </td>
  </tr>
  </tbody>
</table>

CODE I PREVIOUSLY TRIED:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)

attribute_id = 'GPC_material'

table = wait.until(driver.find_element(By.XPATH, "//tr/td[@class ='readonly-cell cell-selected cell-selected-primary sheet-coll']"))

End goal is to search for the ID and 'click' because at that row level I will need to do a could processes such as insert a 'Y' in a cell, click the radio if it is mandatory and insert a number into another cell along the same row of the ID.

Upvotes: 0

Views: 1547

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193098

To locate the element with text as GPC_material you can use either of the following solutions:

  • Using CSS_SELECTOR:

    GPC_material_element = driver.find_element_by_css_selector("table.sheet-table.horizontal-sheet-table.last-row-horizontal-content-page td.readonly-cell.cell-selected.cell-selected-primary.sheet-coll>div.sheet-cell>span")
    
  • Using XPATH:

    GPC_material_element = driver.find_element_by_xpath("//table[@class='sheet-table horizontal-sheet-table last-row-horizontal-content-page']//td[@class='readonly-cell cell-selected cell-selected-primary sheet-coll']/div[@class='sheet-cell']/span[normalize-space()='GPC_material']")
    

Upvotes: 1

Related Questions