簡體   English   中英

量角器/ XPath-查找包含包含文本的后代的元素

[英]Protractor/XPath - Find element that contains a descendant that contains text

我正在尋求有關XPath選擇器的幫助。 我已經了解了XPath的基礎知識,但是很難將它們結合起來解決這一問題。

因此,我有一個HTML網格,其中包含約30行,每行(從左到右)都有一個復選框和員工姓名。 這是行組成的摘要:

<div class="ui-grid-row ng-scope" data-row-num="21"> //Whole row
  <div role="row" ui-grid-row="row" class="ng-isolate-scope">
    <div role="rowheader" id="1746254927-13-urGrid-005-cell"> //This is checkbox
    <div role="gridcell" id="17443224958-13-urGrid-005-cell">
      <div class="ui-grid-cell-contents ng-binding text-left"> Appleseed, Jonny</div>

我的目標是找到:div class =“ ui-grid-row ng-scope” data-row-num =“ 21”>基於其包含'Appleseed,Jonny'的后代。 從這里,我將使用getAttribute提取data-row-num,以便隨后可以導航至右側的復選框。

到目前為止,我有:

 element(by.xpath("//div[@class='ui-grid-row ng-scope') and contains(.//div, 'Appleseed, Jonny')]"));

有人可以幫我指出正確的方向嗎? 我不是在查找包含此文本的元素,而是在查找其父元素,因此可以提取其data-row-num。

或者,如果我可以找到包含“ Appleseed,Jonny”的div元素,那么我又該如何找到其父元素(在這種情況下,它不是直接父元素,而是2個元素)?

給定提供的HTML,此XPath應該可以工作

//div[@class='ui-grid-row ng-scope'][.//div[contains(.,'Appleseed, Jonny')]]

它尋找一個DIV包含指定的類, [@class='ui-grid-row ng-scope']也有一個孩子DIV包含所需的文本, [.//div[contains(.,'Appleseed, Jonny')]]

我希望盡可能(盡可能)具體,例如使用標記名代替*等。

既然您提到過要查找與該行關聯的復選框,那么另一種方法是相對於包含所需文本的元素直接查找該復選框。

//div[@role='rowheader'][.//div[contains(.,'Appleseed, Jonny')]]

由於我假設您將多次使用此XPath,因此將其放入函數中並以字符串形式傳遞所需的文本(例如“ Appleseed,Jonny”)並將其構建到XPath中。

嘗試

//*[contains(text(), "Appleseed, Jonny")]/parent::div[contains(@class,"ui-grid-row ng-scope")].

基本上parent :: *有助於獲取父元素

暫無
暫無

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

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