Python Selenium find Chrome element with attrid (not id)?

Viewed 56

I have a website (private, sorry) that I'm trying to pull some text from. The relevant line, which is nested in a row of a table, when I inspect Chrome is as follows:

<div id="dataGeneral" style="width: auto; height: auto;">
  <table class="someClassName">
    <tbody>
      <tr>
        <td class="*someOtherClassName*" attrid="com.intranet.state"> TextNeeded </td>

I have been able to get the above TextNeeded via full xPath, but is very unstable and most times leads to an error (no such element: Unable to locate element....)

What other method can I use to reliably pull this text? I've tried driver.find_elemend_by_id (and all the other options) using the above attrid, but this also says no such element.

Is there a way to maybe drill down from the table id?

2 Answers

If you want to find this specific locator, use xpath:

   driver.find_element_by_xpath('//tbody/tr/td/a[contains(text(), "TextNeeded")]')

To get a text from this row use:

 text = driver.find_element_by_xpath("//td[contains(@attrid,'com.intranet.state')]").text

You can try this:

wait = WebDriverWait(driver, 10)
element = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@id='dataGeneral']/td[@class='someOtherClassName']")))
print(element.text)

Following imports needed:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Related