You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium Python中如何读取div文本并判断以继续循环?

问题描述

从数据库加载一组唯一ID到列表,将每个ID输入搜索框并点击搜索后,页面会生成HTML数据表。但部分ID不会生成表,而是显示No resuts to display.。需要通过IF语句检查该div标签内的文本,从而跳过当前ID继续处理下一个,但尝试的代码无法捕获该文本。


数据表HTML代码

<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>

尝试的代码

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()")))

完整代码

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
import mysql.connector  # 原代码缺少该导入

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
    }

解决方案

错误原因

  1. Selenium的定位方法(如visibility_of_element_located)仅支持定位元素节点,无法直接定位文本节点(/text()写法不被支持)。
  2. Python语法错误:if语句使用了大括号{},正确语法应为冒号:。

修改后的核心代码(循环部分)

for ids in usdot_ids:
    time.sleep(5)
    # 复用搜索框元素,避免重复定位
    search_box = wait.until(EC.element_to_be_clickable((By.ID, "r1:0:oitx23::content")))
    search_box.clear()
    search_box.send_keys(ids, Keys.RETURN)
    # 点击搜索按钮
    wait.until(EC.element_to_be_clickable((By.ID, "r1:0:cb2"))).click()
    
    # 定位目标div并等待可见
    result_div = wait.until(EC.visibility_of_element_located((By.ID, "r1:0:pc1:tx1::db")))
    # 获取文本并去除首尾空白
    div_text = result_div.text.strip()
    
    # 判断结果并执行对应逻辑
    if div_text == "No resuts to display.":
        print(f"ID {ids} 无结果,跳过")
        continue  # 直接进入下一个ID的处理
    else:
        print(f"ID {ids} 有结果,开始提取数据")
        # 此处编写数据表内容提取逻辑
        # 示例:定位table元素
        table = result_div.find_element(By.TAG_NAME, "table")
        # 后续数据提取操作...

其他优化点

  • 避免重复调用wait.until定位同一元素,提前存储元素对象可提升执行效率。
  • 补充了原代码缺失的mysql.connector导入语句。

内容的提问来源于stack exchange,提问作者Sidath

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 20:55:25