0

I want to click radio button in my automation tests. Even though radio buttons are visible on the page, unselected ones have displayed:false property. Selenide somehow can not click if html object has displayed:false property. The error is : Element should be visible {By.id: radio_btn_id}

Here is my radio button:

<input class="radio_class" id="radio_btn_id" name="radio_btn_name" type="radio" value="12" displayed:false></input>

What I have tried for removing the property and none of them are working

SelenideElement element = $(By.id(id));
Selenide.executeJavaScript("document.getElementById('radio_btn_id').removeAttribute('displayed:false')", element);

Selenide.executeJavaScript("document.getElementById('radio_btn_id').removeAttribute(\"displayed:false\")", element);

Selenide.executeJavaScript("jQuery('select:not(:visible)').css('display','block')", element);

I tried to remove selected:true property and it worked. I don't know why it does not work for displayed:false. Do any of you have an idea ?

[EDIT] Accepted answer is Selenium version. In Selenide it is more clean and simple :

SelenideElement element = $(By.id(id));
Selenide.executeJavaScript("document.getElementById('"+ id+ "').click();", element);

[SOLUTION]

Selenide behaviors to check-boxes&radio buttons

fiskra
  • 872
  • 1
  • 9
  • 19
  • Is that real code? `displayed:false` is not valid html. Do you mean `display` (the css style property) instead of some custom "displayed" html attribute? – kapex Oct 09 '18 at 12:08
  • I have tried manually and works from the console. Can you test it over browser console and see if the displayed attribute is being removed. document.getElementById('radio_btn_id').removeAttribute('displayed:false') – Infern0 Oct 09 '18 at 12:19
  • @Infern0 The problem is, if you check the element in browser console, you can not see this property. I have just realized selenium puts this property automatically. And I have seen also opacity:0 property and when I run this : Selenide.executeJavaScript("document.getElementById('"+ id+ "').style.opacity= 1", element); I could remove also disabled:false . But it does not make it clickable, still. And I don't like the approach because I am changing page design in this way which is nonsense for automation tests. – fiskra Oct 09 '18 at 12:24
  • @kapex you are right. 'display' and 'displayed:false' are totally different and I think 'displayed:false' is a custom selenium property. It is not a default html property – fiskra Oct 09 '18 at 12:27
  • @fiskra, selenium does not add any kind of attributes/parameters etc. about Selenide i don't know. Did you try to execute js click over the element ? – Infern0 Oct 09 '18 at 12:33
  • @Infern0 maybe Selenide does. I am not sure really. But it is weird that I see the html element differently in page and code. I tried with js click and it works without changing any property. Thanks for the suggestion. You can write as a solution. – fiskra Oct 09 '18 at 12:40

1 Answers1

0

Click the element with JS executor:

public void clickElementWithJS(By locator) {
    String jsClickCode = "arguments[0].scrollIntoView(true); arguments[0].click();";
    try {
        WebElement elementToClick = driver.findElement(locator);
        ((JavascriptExecutor) driver).executeScript(jsClickCode, elementToClick);
    } catch(Exception e) {
        System.out.println("Element could not be clicked.. "  + e.getMessage());
    }
}
Infern0
  • 2,565
  • 1
  • 8
  • 21