Toffa
Toffa

Reputation: 45

click second radio button with java selenium

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();

Upvotes: 4

Views: 1299

Answers (4)

Gaurav Gautam
Gaurav Gautam

Reputation: 11

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.

Upvotes: 1

akhilesh gulati
akhilesh gulati

Reputation: 128

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);
        }

Upvotes: 0

Saurabh Gaur
Saurabh Gaur

Reputation: 23805

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);

Upvotes: 2

Florent B.
Florent B.

Reputation: 42518

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();

Upvotes: 2

Related Questions