How to read text inside a div tag in selenium python?

Question:

I’m trying to load data for a unique id set that loads from the database to a list. when eneter the id into a search textbox and clicking on the search button it will generate an HTML data table. But Some ids do not create the data table. it will show a message "No resuts to display." Now I need to continue the for loop to the next id to generate the table. How can I check "No resuts to display." inside the div tag with the IF statement?

HTML Code for the table

<div id="r1:0:pc1:tx1::db" class="x17e">
  <table role="presentation" summary="" class="x17f x184">
    <colgroup span="10">
      <col style="width:143px;">
      <col style="width:105px;">
      <col style="width:105px;">
      <col style="width:145px;">
      <col style="width: 471px;">
      <col style="width:145px;">
      <col style="width:105px;">
      <col style="width:105px;">
      <col style="width:105px;">
      <col style="width:105px;">
    </colgroup>
  </table>
  No resuts to display.
</div>

I tried it with the following code. But can’t catch the text.

ele = driver.find_elements("xpath", "//*[@id='r1:0:pc1:tx1::db']/text()")
print(ele)

table = WebDriverWait(driver, 30).until(
        EC.visibility_of_element_located((By.XPATH, "//*[@id='r1:0:pc1:tx1::db']/text()")))

This is my full code:

from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time


options = Options()
options.add_experimental_option("detach", True)

webdriver_service = Service('/WebDriver/chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 30)

url = "https://"

try:
    connection = mysql.connector.connect(host='localhost', database='test_db', user='root', password='456879')

    mySql_select_query = """SELECT usdot_id FROM company_usdot WHERE flag=0 """

    cursor = connection.cursor()
    cursor.execute(mySql_select_query)

    usdot_ids = [i[0] for i in cursor.fetchall()]
    print(usdot_ids)


except mysql.connector.Error as error:
    print("Failed to select record in MySQL table {}".format(error))

finally:
    if connection.is_connected():
        cursor.close()
        connection.close()
        print("MySQL connection is closed")
        
driver.get(url)

for ids in usdot_ids:
    time.sleep(5)
    wait.until(EC.element_to_be_clickable((By.ID, "r1:0:oitx23::content"))).clear()
    wait.until(EC.element_to_be_clickable((By.ID, "r1:0:oitx23::content"))).send_keys(ids, Keys.RETURN)
    wait.until(EC.element_to_be_clickable((By.ID, "r1:0:cb2"))).click()
    
    ele = driver.find_elements("xpath", "//*[@id='r1:0:pc1:tx1::db']/text()")
    print(ele)

    table = WebDriverWait(driver, 30).until(
        EC.visibility_of_element_located((By.XPATH, "//*[@id='r1:0:pc1:tx1::db']/text()")))

    if(table.text =="No resuts to display."){
        #need to continue the loop
    }
    else{
        #get the details from the data table
    }
Asked By: Sidath

||

Answers:

  1. Selenium doesn’t support xpath: //*[@id='r1:0:pc1:tx1::db']/text() like this.You need to change this.
  2. Inside the div tag you have table tag as well so instead of == better use in clause.
  3. Python if..else block is different

Code:

for ids in usdot_ids:
    time.sleep(5)
    wait.until(EC.element_to_be_clickable((By.ID, "r1:0:oitx23::content"))).clear()
    wait.until(EC.element_to_be_clickable((By.ID, "r1:0:oitx23::content"))).send_keys(ids, Keys.RETURN)
    wait.until(EC.element_to_be_clickable((By.ID, "r1:0:cb2"))).click()
    
    ele = driver.find_elements("xpath", "//*[@id='r1:0:pc1:tx1::db']")
    print(ele)

    table = WebDriverWait(driver, 30).until(
        EC.visibility_of_element_located((By.XPATH, "//*[@id='r1:0:pc1:tx1::db']")))

    if "No resuts to display." in table.text:
        #need to continue the loop
    
    else:
        #get the details from the data table
    
Answered By: KunduK