Home » excel » Need help to select radio button in Chrome (Part II) using Excel VBA Selenium Basic

Need help to select radio button in Chrome (Part II) using Excel VBA Selenium Basic

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m struggling once more trying to select a radio button in Chrome (v 75.0.3770.100) using Selenium Basic ChromeDriver (v 75.0.3770.140) in Excel (2013) VBE. Here’s the HTML:

<div class=radio-wrap>
<input id="shipToSelect" type="radio" class="radio" 
name="soldToShipToselection" value="SH" ng-click="radioClicked($event)">
<label for="shipToSelect" class="radio-label">
<i></i>
<span>Ship-to</span>
</label>
</div>

I’ve tried unsuccessfully with the below:

obj.FindElementByCss("div.radio-wrap>input.radio[id$='radio']").Click
obj.FindElementByName("soldToShipToselection").Click
obj.FindElementById("shipToSelect").Click
obj.FindElementByXPath("//div[@class='radio-wrap']/input[.,(@id,'shipToSelect')]").Click
How to&Answers:

To click on the radio button associated with the text as Ship-to you can use either of the following Locator Strategies:

  • cssSelector:

    driver.FindElementByCss("label[for='shipToSelect']").Click
    
  • xpath:

    driver.FindElementByXPath("//label[@for='shipToSelect']").Click
    

Note: As it is a JavaScript enabled element you need to induce a waiter for the element to be clickable


Update

As an alternative, you can try either of the folloing Locator Strategies:

  • cssSelector:

    driver.FindElementByCss("input.radio#shipToSelect[name='soldToShipToselection']").Click
    
  • xpath:

    driver.FindElementByXPath("//input[@class='radio' and @id='shipToSelect'][@name='soldToShipToselection']").Click
    

Answer:

Try the following Xpath to click on radio button.Use Explicit wait before interacting the element.

obj.FindElementByXPath("//div[@class='radio-wrap']/input[@id='shipToSelect']").Click