DOSKrupp
DOSKrupp

Reputation: 240

How to click a parent element using the child element for the following scenario

I am using selenium webdriver to automate. I have a special case below.

<div id = "A">
  <div id = "container">
    <div id="innercontainer">
      <div>
       <div id="ruleContainer">
         <span id="rule">CNET</span>
         <div id="name">CNET></div>
       </div>
    </div>
  </div>
  <a href = "#....."></a>
</div>
<div id = "A">
  <div id = "container">
    <div id="innercontainer">
      <div>
       <div id="ruleContainer">
         <span id="rule">GNET</span>
         <div id="name">GNET></div>
       </div>
    </div>
  </div>
  <a href = "#....."></a>
</div>`<div id = "A">
  <div id = "container">
    <div id="innercontainer">
      <div>
       <div id="ruleContainer">
         <span id="rule">DNET</span>
         <div id="name">DNET></div>
       </div>
    </div>
  </div>
  <a href = "#....."></a>
</div>`

Here I need to click on element A with text CNET... I am able to get to the child CNET but it is a dead element. So I need to click on anchor for element A having that particular child.

How can I do that? Is there a way? I know the solution for looping but my application refreshes so often and I encounter stale exception because of it. So could some one give me a better solution like navigating back to the parent and then to the sibling and click().

Upvotes: 2

Views: 3481

Answers (1)

drkthng
drkthng

Reputation: 6909

try this xpath:

//div[@id='A' and .//span[contains(text(), 'CNET')]]//a

it searches for the div with id = 'A' that has a span that contains the text 'CNET', from that div it selects the anchor-child-element

Upvotes: 7

Related Questions