簡體   English   中英

Xpath Selenium-如何通過子文本定位元素

[英]Xpath Selenium- How to locate a element by the sub text

希望你真的很好,可以幫助我解決這個簡短的問題。 我試圖找到以下對象id=C39_W133_V136_thtmlb_button_27但使用位於跨度之后的文本(文本 =“編輯”)。 請我嘗試了不同的方法,但直到現在都沒有用,知道嗎?

<a href="javascript:void(0)" class="th-bt th-bt-icontext-dis icon-font" tabindex="-1" oncontextmenu="return false;" ondragstart="return false;" id="C39_W133_V136_thtmlb_button_27">
  ::before  
  <img class="th-bt-img" src="/SAP/BC/BSP/SAP/thtmlb_styles/sap_skins/belize/images/1x1.png">  
  <span class="th-bt-span"><b class="th-bt-b">Edit</b></span>  
  <b class="th-bt-b">Edit</b>
</a>

為了使用元素中包含的文本定位元素,唯一的選擇是使用 XPath。

//a[./b[.='Edit']]
^ Start at the top of the document and find an A tag
   ^ ...that has a descendant B tag
        ^ ...that contains the text 'Edit'

要定位具有下降<span>文本作為Edit<a>元素,您需要為visibility_of_element_located()引入WebDriverWait ,您可以使用以下任一定位器策略

  • 使用XPATH

     element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[contains(@id, 'thtmlb_button')][.//b[text()='Edit']]")))
  • 注意:您必須添加以下導入:

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

暫無
暫無

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

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