2

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().

DOSKrupp
  • 240
  • 4
  • 15
  • Here's a related post about clicking the parent element, for once you have found the element with 'CNET' text. http://stackoverflow.com/questions/8577636/select-parent-element-of-known-element-in-selenium . As a side note, as I'm not super familiar with selenium but you probably don't want to have multiple elements with the same ID='A', instead you could use a semantic class name like 'container-parent' or something similiar. – clovola Nov 06 '15 at 18:40
  • seems there is one div too much opened in every id='A' area – drkthng Nov 06 '15 at 19:49

1 Answers1

7

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

drkthng
  • 6,651
  • 7
  • 33
  • 53