在 Python Selenium 中使用 Xpath(没有唯一标识符)查找值

Finding value using Xpath (with no unique identifiers) in Python Selenium

我在尝试使用 Python 在 Selenium 中获取车辆颜色时遇到问题。我检查了 YouTube、Whosebug 和所有常用资源,但似乎找不到有意义的答案(我对 Python 和 Selenium 比较陌生)。我目前正在进行一个项目,根据已存在于价差 sheet 上的车辆登记号,自动将车辆颜色从 gov.uk 网站提取到 excel sheet .代码还没有写完,我想先跨过这个 Xpath 障碍!

我需要从此代码中获取 'Blue' 值:

<dl class="summary-no-action">
<div class="govuk-summary-list__row">
<dt>Registration number</dt>
<dd>
<div class="reg-mark-sm">WJ06 HYF</div>
</dd>
</div>
<div class="govuk-summary-list__row">
<dt>Make</dt>
<dd>VOLKSWAGEN</dd>
</div>
<div class="govuk-summary-list__row">
<dt>Colour</dt>
<dd>BLUE</dd>
</div>
</dl>

但是,如您所见,由于没有特定的 ID、class、标签名称等可使用,所以我假设 Xpath 是我唯一的选择?谁能帮我最好地实施这个?我的假设是在 'Colour' 下找到第一个 'dd' 标签,但我不知道如何写这个! 这是我目前正在处理的代码片段:

try:
main = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CLASS_NAME, "summary-no-action"))
    )

div = main.find_element(By.LINK_TEXT, "Colour")
for article in div:
    header = article.find_element(By.TAG_NAME, "dd")
    print(header.text)
finally:
driver.quit()

我知道 'div = main.find_element(By.LINK_TEXT, "Colour")' 行不正确,但我需要用一些东西替换它,以便我可以获取下方 'dd' 标签中的颜色。

这是我原来的,但它带回了“summary-no-action”class名称中的所有值:

try:
main = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CLASS_NAME, "summary-no-action"))
    )

div = main.find_elements(By.CLASS_NAME, "govuk-summary-list__row")
for article in div:
    header = article.find_element(By.TAG_NAME, "dd")
    print(header.text)
finally:
driver.quit()

如有任何帮助,我们将不胜感激!

编辑:

作为参考,这里是完整的代码:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

driver =     
webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.google.com")

driver.get ("https://vehicleenquiry.service.gov.uk/")

time.sleep(5)

search = driver.find_element(By.ID ,                 
"wizard_vehicle_enquiry_capture_vrn_vrn")
search.send_keys("wj06hyf")
search.send_keys(Keys.RETURN)

try:
main = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CLASS_NAME, "summary-no-action"))
    )

div = main.find_elements(By.CLASS_NAME, "govuk-summary-list__row")
for article in div:
    header = article.find_element(By.TAG_NAME, "dd")
    print(header.text)
finally:
driver.quit()

使用下面的xpath得到值BLUE。首先用文本 colour 和后面的 dd 标签

识别 dt 标签
//dt[text()='Colour']/following::dd[1]

代码:

print(WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH, "//dt[text()='Colour']/following::dd[1]"))).text)

要获取文本 Blue,您需要引入 WebDriverWait for the and you can use either of the following :

  • 使用CSS_SELECTORget_attribute("innerHTML"):

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "dl.summary-no-action div:last-child dd"))).get_attribute("innerHTML"))
    
  • 使用 XPATHtext 属性:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//dl[@class='summary-no-action']//div[@class='govuk-summary-list__row']/dt[text()='Colour']//following-sibling::dd[1]"))).text)
    
  • 注意:您必须添加以下导入:

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