-1

Wondering is anybody can help me finding an xpath for the following element on a page

I am creating a test for the following question - I have been able to find an xpath for the Q1 is registered user portion but I would like to find an xpath which selects is reigstered user and also the Yes answer

enter image description here

Below is the HTML of the element

<div ng-show="resultsVm.eligibilityResults.questionAnswerPairs.length &amp;&amp; doesBusinessExist" class="row show-grid" ng-repeat="eligibilityResult in resultsVm.eligibilityResults.questionAnswerPairs">
        <div class="col-md-8">
            <label>
                Q1. Is Registered User?
            </label>
        </div>
        <div class="col-md-2">
            <span class="glyphicon glyphicon-ok" ng-class="{
              'glyphicon-ok' : (eligibilityResult.answer.answerCode == 'Y')
              , 'glyphicon-remove' : (eligibilityResult.answer.answerCode == 'N')
              , 'glyphicon-flag' : (eligibilityResult.answer.answerCode == 'O')
              }" aria-hidden="true"></span>  Yes
        </div>
    </div>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
AlpineF30
  • 95
  • 2
  • 7

2 Answers2

0

To click on the element with text as Yes you can use either of the following Locator Strategies:

  • Using Java and xpath:

    driver.findElement(By.xpath("//label[contains(., 'Is Registered User')]//following::div[./span[contains(@ng-class, 'answerCode')]]")).click();
    
  • Using Python and XPATH:

    driver.find_element(By.XPATH, "//label[contains(., 'Is Registered User')]//following::div[./span[contains(@ng-class, 'answerCode')]]").click()
    

Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using Java and xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[contains(., 'Is Registered User')]//following::div[./span[contains(@ng-class, 'answerCode')]]"))).click();
    
  • Using Python and XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[contains(., 'Is Registered User')]//following::div[./span[contains(@ng-class, 'answerCode')]]"))).click()
    
  • Note: For Python clients you have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Use following xpaths:

To get question:

//label[contains(text(),"Q1. Is Registered User?")]

To get answer of specific question:

//label[contains(text(),"User")]/ancestor::div[@class="row show-grid"]//*[text()[contains(.,"Yes") or contains(.,"No")]]

Here we are getting the parent of the question and then go back to the answer

PDHide
  • 18,113
  • 2
  • 31
  • 46