4

i want to klick on the second radio button with java/selenium. Ids are dynamic and i dont know why xpath dont work. It would be really helpful if you guys can show me how this works.

HTML

   <div class="form-radiobutton-group group-horizontal" id="id29">
            <div class="form-radiobutton-element">
                <span class="form-radiobutton-wrapper">
                    <input class="salutation_f feedback-panel-trigger wicket-id29" id="id4" name="personaldataPanel:salutation:choices" value="radio9" type="radio">
                    <label for="id4" class=""></label>
                </span>
                <label for="id4">
                    Frau
                </label>
            </div>
            <div class="form-radiobutton-element">
                <span class="form-radiobutton-wrapper">
                    <input class="salutation_m feedback-panel-trigger wicket-id29" id="id3" name="personaldataPanel:salutation:choices" value="radio11" type="radio">
                    <label for="id3" class=""></label>
                </span>
                <label for="id3">
                    Herr
                </label>
            </div>
        </div>

Code right now

WebElement m = driver.findElement(By.xpath("//div[2]/span/input"));
m.click();

4 Answers 4

2

You can locate radio button using By.xpath with their label text as below :-

  • To click radio button with the label text Frau :

    driver.findElement(By.xpath("//input[../following-sibling::label[contains(.,'Frau')]]")).click();
    
  • To click radio button with the label text Herr :

    driver.findElement(By.xpath("//input[../following-sibling::label[contains(.,'Herr')]]")).click();
    

Edited :- If you are getting exception that click would receive by other element, need to implement WebDriverWait to wait until element visible on DOM as below :-

WebDriverWait wait = new WebDriverWait(driver, 10);
el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[../following-sibling::label[contains(.,'Herr')]]")));
el.click();

If you are still facing same issue then try to click using JavascriptExecutor as below :-

((JavascriptExecutor)driver).executeScript("arguments[0].click()", el);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for answer but i get this error Caused by: org.openqa.selenium.WebDriverException: Element is not clickable at point (358.5, 262). Other element would receive the click: <label for="id4" class=""></label>
@Toffa I think you need to implement WebDriverWait to wait until element present on the dom..:)
god thank you, update works ! I would never have finished without your help.
@Toffa you welcome... Glad to help you. please accept the answer as well if it helped...meta.stackexchange.com/questions/5234/…)
2

To click the radio button with the label "Frau":

WebElement m = driver.findElement(By.xpath(
    "//input[@id=//label[normalize-space(.)='Frau']/@for]"));

m.click();

Or:

WebElement m = driver.findElement(By.xpath(
    "id(//label[normalize-space(.)='Frau']/@for)"));

m.click();

Comments

1
driver.findElement(By.xpath("//*[text()='the label text']").click();

by this way, you can find out the name of the label and click on the particular radio button by this.

Comments

0
element=//input[@id=//label[normalize-space(.)='Frau']/@for]

public void javascriptclick(String element)
    { 
        WebElement webElement=driver.findElement(By.xpath(element));
        JavascriptExecutor js = (JavascriptExecutor) driver;

        js.executeScript("arguments[0].click();",webElement);   
        System.out.println("javascriptclick"+" "+ element);
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.