0

I'm trying to switch to a popup on bootstrap modal webpage and it doesn't allow the click on a radio button. The page has only 1 frame therefore I cannot switch to another frame.

My element is:

input class type="radio"
"Create the account myself"

I'm using XPath:

driver.findElement(By.xpath("//input[@class type = 'radio' and contains(text(), 'Create the account myself')]")).click();

Please assist.

  • Please provide the relevant HTML for the element. – VanderLinden Nov 13 '17 at 16:22
1

Modal Switching

Bootstrap modals are not iFrames, and as such, there is no need to "switch" the driver context to them. You should have no problem locating modal elements as you would any other element in the DOM.

If you want to locate elements within the modal, I have found the following approach to be useful.

WebElement modal = driver.findElement(By.xpath("<xpath to modal container here>"));
WebElement child = modal.findElement(By.xpath("<relative xpath to child element here"));

Where the child xpath begins with .//. The leading dot indicates to "search from the current node". Since we are calling findElement from the context of the modal container, the search will only return matches that are children of the modal. By.id, By.className, etc. will do the same when called from the context of an element.

XPath Issue

The portion of your XPath @class type = 'radio' isn't quite right. @type is the attribute you should be trying to match on. @class appears to be empty (I would have to see the actual HTML to be sure).

Try the following

The final issue is that the text you are searching for actually belongs to the <label> element that is the direct parent of the radio button, so we first must locate the label based on the specified text, and then get the child radio element. The code below should do what you're looking for.

WebElement modal = driver.findElement(By.xpath("//div[contains(@class,'modal-body')]"));
WebElement radio = modal.findElement(By.xpath(".//label[@class='radiogroup-element' and contains(@style,'block') and text()='Create the account myself']/input[@type='radio']"));
radio.click();

As in the generic example above, it will first locate the modal. Then it will find the intended radio button from the context of the modal and click it.

|improve this answer|||||
  • You are right VanderLinden, @class is empty. The problem though is switching to the pop up – Thabiso Ditabe Nov 13 '17 at 16:53
  • You say "the page only has 1 frame therefore I cannot switch to another frame". Why are you trying to switch to another frame? Is the element in question a child of an <iframe> element in the DOM? Just because the modal appears to "pop up" doesn't necessarily mean it is in a separate frame. – VanderLinden Nov 13 '17 at 17:01
  • I've updated my answer to address the problem you indicated with frame switching. If you are still unable to resolve the issue, you will probably need to edit your question with the relevant HTML to get more assistance. – VanderLinden Nov 13 '17 at 17:24
  • <div class="modal-body GEHR2AJDJO" style="height: 340px;"><h6>What would you like to do?</h6><div class="row"><div class="col-md-24"><div><div><label class="radiogroup-element" style="display: block;"><input class="" type="radio">Create the account myself</label><label class="radiogroup-element" – Thabiso Ditabe Nov 13 '17 at 17:27
  • I have just added the HTML – Thabiso Ditabe Nov 13 '17 at 17:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.