1

I am dealing with Selenium-Java. For that I need to perform an automatization on a web page.

After recieving very good feedback last time I do have the challenge to find one of the objects I would like to click on.

The following HTML code defines the structure on the page:

<div class="cmp-answers" data-evaluation="0" data-style="sc" data-submit-on-answer-click="0" data-columns="1">
    <label class="cmp-answer correct disabled">
        <input type="radio" name="answer3425" data-question-id="3425" data-answer-id="16882" value="1" checked="" disabled="" data-misc-answer="0" autocomplete="off">
        <div class="cmp-answer-ui">
            <span class="cmp-answer-label"><p>Anwer No 1</p></span>
            <span class="cmp-answer-feedback"></span>
        </div>
    </label>
    <label class="cmp-answer  disabled">    
        <input type="radio" name="answer3425" data-question-id="3425" data-answer-id="16884" value="1" disabled="" data-misc-answer="0" autocomplete="off">
            <div class="cmp-answer-ui">     
                <span class="cmp-answer-label"><p>Answer No 2</p></span>
                <span class="cmp-answer-feedback"></span>
            </div>
    </label>
    <label class="cmp-answer  disabled">
        <input type="radio" name="answer3425" data-question-id="3425" data-answer-id="16883" value="1" disabled="" data-misc-answer="0" autocomplete="off">
            <div class="cmp-answer-ui">
                <span class="cmp-answer-label"><p>Answer No 3</p></span>
                <span class="cmp-answer-feedback"></span>
            </div>
    </label>
</div>

These kindes of radio buttons provide three different options to choose:

  • Answer No1
  • Answer No2
  • Answer No3

For test reasons I would do the automatic click on Answer No1.

Via the html analysis I copied the xPath as well as the CSS finder information.

I used this information within the findElement method like:

driver.findElement(By.cssSelector(...));`

or

driver.findElement(By.xpath(...));`

I copied the xPath for different objects defined by the HTML code shown above and used it within the findElement method. Unfortunately none of these options leads to the fact that I was able to find or indicate the object via Selenium.

Therefore, my question. Is anyone of you experienced with that or faced the same or similar chalange? Is there any advice or solution how I can find the object via Selenium an (in the next step) click on that button.

Thank you in advance for any help.

As arguments I used the output of the html analysis (copy->xpath / copy-> css).

Moreover, in the next step I would like to execute click() after finding the element itself.

Destroy666
  • 892
  • 12
  • 19
Jocken
  • 15
  • 4

1 Answers1

0

There are a few common situations you may be running into that are causing your issue:

  1. You need to add a wait. It's possible the page loads slowly and your code is attempting to find the element on the page before it actually appears.

  2. The button is in an IFRAME. See the docs on how to handle this.

  3. The button is in a shadow-root. See java.lang.NullPointerException error when trying to locate a shadow element with Selenium for info on how to handle this.

Given the HTML you provided, the locator below should work. I've added a wait.

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='cmp-answer-label']/p[text()='Anwer No 1']"))).click();
JeffC
  • 22,180
  • 5
  • 32
  • 55
  • Thank you for your answer. I tried your code regarding the locator but it does not work. Moreover, I used it kobination with finding the frame you mentioned under 2. Unfortionally I am still not able to find the button and in the next step to click it. Do you have any further ideas what to try or test? – Jocken Apr 22 '23 at 05:20
  • What was the error message? Is 'Anwer No 1' the actual text of the answer with "answer" spelled incorrectly? – JeffC Apr 22 '23 at 05:24
  • 1
    thank you for your help. Your suggestion regarding the xpath solved the problem in combination with two steps in advance: "Scrall Down" & Select Frame (that refers to the 2nd bullet point you mentioned). Thank you, thank you. – Jocken Apr 23 '23 at 20:07