0

I have searched through multiple questions like that but they don't seem to solve my problem. I am writing an automated test and need to access a particular checkbox related to a text from several of them on the page.

<div _ngcontent-vcu-c32="" class="table-row-wraper ng-star-inserted">
      <div _ngcontent-vcu-c32="" class="custom-control custom-checkbox center-absolute">
        <input _ngcontent-vcu-c32="" class="custom-control-input" type="checkbox" id="assign10000374">
        <label _ngcontent-vcu-c32="" class="custom-control-label" for="assign10000374"></label>
        </div>
      <ls-cross-api-lead-item _ngcontent-vcu-c32="" _nghost-vcu-c35="" class="ng-tns-c35-4">
        <div _ngcontent-vcu-c35="" class="table-row-border">
          <div _ngcontent-vcu-c35="" class="table-row table-row-min-height">
            <div _ngcontent-vcu-c35="" class="col-2">
              <span _ngcontent-vcu-c35="" class="d-block">Wednesday</span>
              <span _ngcontent-vcu-c35="" class="d-block">30.09.2020</span>
            </div>
            <div _ngcontent-vcu-c35="" class="col-2"> CREDIT </div>
      </ls-cross-api-lead-item>
    </div>

I need to find the /input of the checkbox based on the initial div containing text "CREDIT". In my best knowledge this should do the work:

(xpath = "//ls-cross-api-lead-item//child::div//div[contains(text(), 'CREDIT')]//parent::div/div[@class='custom-checkbox']/input")

But it doesn't see the element to click.

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element

Any ideas what am I doing wrong?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
AlePie
  • 9
  • 1

3 Answers3

0

You can try something like this:

//input[./parent::div/parent::div//div[contains(., 'CREDIT')]]

Test: http://xpather.com/vklAaFbI

Alexey R.
  • 8,057
  • 2
  • 11
  • 27
0

To click on the input element checkbox based on the text CREDIT induce WebDriverWait() and wait for elementToBeClickable() and following xpath options.

XPATH1:

WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement elementcheckbox = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='table-row-wraper ng-star-inserted'][.//div[contains(.,'CREDIT')]]//input")));
elementcheckbox.click();

XPATH2:

WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement elementcheckbox = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='table-row-wraper ng-star-inserted'][.//div[contains(.,'CREDIT')]]//input[@class='custom-control-input']")));
elementcheckbox.click();
KunduK
  • 32,888
  • 5
  • 17
  • 41
0

To click on the <input> element with reference to the <div> element with text as CREDIT you can use the following based Locator Strategy:

  • Using xpath and normalize-space():

    driver.findElement(By.xpath("//div[normalize-space()='CREDIT']//ancestor::ls-cross-api-lead-item//preceding::input")).click();
    
  • Using xpath and contains():

    driver.findElement(By.xpath("//div[contains(., 'CREDIT')]//ancestor::ls-cross-api-lead-item//preceding::input")).click();
    

However, as the element is an Angular element so to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • Using xpath and normalize-space():

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[normalize-space()='CREDIT']//ancestor::ls-cross-api-lead-item//preceding::input"))).click();
    
  • Using xpath and contains():

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(., 'CREDIT')]//ancestor::ls-cross-api-lead-item//preceding::input"))).click();
    

Reference

You can find a detailed discussion on NoSuchElementException in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352