-1

I have:

    (1)
    <div>
     <h5> unique name1</h5>
     <div>
      <div>
       I don't want click this div.
      </div>
     </div>
    </div>
    (2)
    <div>
     <h5> unique name2</h5>
     <div>
      <div>
       I want click this div.
      </div>
     </div>
    </div>

how I can find div to click in selenium? Very important is this... Sometimes my div is (2) sometimes (3) e.t.c . Need to use h5 to find it.

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • 1
    You can use xpath: `//h5[contains(text(), 'unique name2')]/following-sibling::div/div` – KunLun Oct 09 '19 at 12:07
  • You can use below xpath as well : `//h5[contains(., 'unique name2')]/following-sibling::div[1]/div` – KunduK Oct 09 '19 at 12:16

2 Answers2

1

To locate & click the desired div element under your h5, you can use the following XPath:

// declare a wait so we can wait on element to exist
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));

// wait for element to exist
var elementToClick = wait.Until(
    ExpectedConditions.ElementExists((By.XPath("//h5[text()='unique name2']/following-sibling::div/div"))));

// click element
elementToClick.Click();

Because wait.Until returns the WebElement that is being waited on, you can actually merge the last two lines into a single line:

// wait for element to exist, then click
wait.Until(
    ExpectedConditions.ElementExists((By.XPath("//h5[text()='unique name2']/following-sibling::div/div")))).Click();

Christine
  • 5,567
  • 2
  • 12
  • 35
  • With this approach, you are hitting the page twice, once for the wait (which only indicates presence, not if it's clickable) and once to click it. In your wait, wait for clickable instead which returns the element found and then click it, e.g. `wait.Until(ExpectedConditions.ElementToBeClickable(...).Click();`. Now you've only hit the page once, waited until the element is clickable, and then clicked it. – JeffC Oct 10 '19 at 20:24
  • 1
    True -- good suggestion, as `wait.Until` does return the element being searched for. The reason I split into separate lines was to add clarity to the solution -- if a user does not know that `wait.Until` returns a WebElement, the solution can be confusing for them, so 2 separate lines made sense from that perspective. I'll update my answer accordingly. – Christine Oct 10 '19 at 20:28
  • 1
    Fair enough, if you wanted to keep the two lines separate (for better clarity), you can do that and still hit the page only once, e.g. `IWebElement e = wait.Until(ExpectedConditions.ElementToBeClickable(...)); e.Click();`. – JeffC Oct 10 '19 at 20:33
0

To invoke click() on the <div> with text as I want click this div you have to induce WebDriverWait for the ElementToBeClickable() and you can use either of the following Locator Strategies:

  • Using XPATH:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//h5[contains(., 'unique name2')]//following::div[1]/div"))).Click();
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217