如何在selenium python中读取div标签内的文本?



我试图加载数据的唯一id集,从数据库加载到一个列表。当在搜索文本框中输入id并单击搜索按钮时,它将生成一个HTML数据表。但是有些id不创建数据表。它将显示一条消息"没有结果显示。"现在我需要继续对下一个id执行for循环以生成表。如何检查"无结果显示"。div标签内的IF语句?

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

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不支持这样的xpath: //*[@id='r1:0:pc1:tx1::db']/text()。你需要改变这个
  2. 里面的div标签,你有table标签,以及,而不是==更好地使用在子句。
  3. Python if..else块是不同的
  4. 代码:

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

最新更新