簡體   English   中英

如何選擇具有此功能的 div

[英]how do I select the div that has this <i> using Selenium python

我想使用 python 用 selenium 進行測驗,這是 1 個答案選擇的代碼。

當它是正確答案時,遺囑包含這個特定的類,當它不正確時,它是另一個類

所以我想得到答案正確的問題文本(產品對客戶滿意度的影響。)這將取決於 < i > 里面的類(因為正如我所說,當它的錯誤答案時,它是里面的另一個類)

但我找不到如何使用 XPATH 選擇它

<div class='course-player__interactive-checkbox _interactive-checkbox_cjbh9b'>
  <div class='course-player__interactive-checkbox__choice-label _interactive-checkbox__choice-label_cjbh9b'>
    <span class='course-player__interactive-checkbox__choice-letter _interactive-checkbox__choice-letter_cjbh9b'> ANSWER A </span>
  </div>
  <span class='course-player__interactive-checkbox__choice-text _interactive-checkbox__choice-text_cjbh9b'> The product's impact on customer satisfaction. </span>
  <span class='course-player__interactive-checkbox__choice-answered _interactive-checkbox__choice-answered_cjbh9b'>
    <i class='toga-icon toga-icon-circle-fill-check _interactive-checkbox__choice-icon_cjbh9b'> </i>
  </span>
</div>

一種檢索字符串的方法

產品對客戶滿意度的影響。

Selenium 如下:

from selenium import webdriver
driver = webdriver.Chrome()
driver.get("file:///input.html")
lookup = driver.find_elements_by_xpath("//div/span[i]/preceding-sibling::*[1]")
print(lookup[0].text)

XPath-1.0 表達式選擇<span>之前的<span> ,它有一個<i>子節點。
但請注意,此代碼不包含任何錯誤處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM