Selenium code to select switch

I do automation using selenium, I need help in choosing how to choose a radio button. If possible, help me with selenium java code.

+8
selenium
source share
7 answers

Assuming you have selenium, configure it simply:

selenium.click('radio button locator'); 

You can look at selenium javadoc http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/java/com/thoughtworks/selenium/Selenium.html

+7
source share
 click > xpath=(//input[@type='checkbox'])[position()=1] click > xpath=(//input[@type='checkbox'])[position()=2] click > xpath=(//input[@type='checkbox'])[position()=3] click > xpath=(//input[@type='checkbox'])[position()=4] 

etc .... use these commands to select random and any switch

+1
source share
 public class radioExamJavaScr { public static void main(String[] args) throws IOException { WebDriver driver = new FirefoxDriver(); EventFiringWebDriver dr = new EventFiringWebDriver(driver); dr.get("http://www.makemytrip.com/"); dr.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); ((JavascriptExecutor)dr).executeScript("document.getElementById('roundtrip_r').click();"); WebElement one_way = (WebElement)((JavascriptExecutor)dr).executeScript("return document.getElementById('oneway_r') ;"); System.out.println(one_way.isSelected()); WebElement round_trip = (WebElement)((JavascriptExecutor)dr).executeScript("return document.getElementById('roundtrip_r') ;"); System.out.println(round_trip.isSelected()); } } 

In the above example, I select the radio button with "ROUND TRIP" using "JavaScript".

The last four lines should check and see if the expected switch on the page is selected or not.

NOTE. I give a simple solution to solve the problem (radio selection) on the selected web page. You can write better code. (the user can write a method to receive the radio ID and skip the entire existing radio button to see which one is selected).

0
source share

I am using this method:

 String radioButtonId = "radioButtonId"; selenium.focus("id=" + radioButtonId); selenium.click("id=" + radioButtonId, "concreteRadioButtonValue"); 
0
source share

What you can do is:

Create a method with the return type WebElement and use the findElement () method. Example:

 WebDriver test; test = new FirefoxDriver(); public static WebElement checkAndGet(By b) throws Exception { return test.findElement(b); } 

Save the WebElement and use the click () method. Example:

 WebElement radiobutton = checkAndGet(By.xpath("//span[@class='label ng-binding']"); radiobutton.click(); 

Hope this helps!

0
source share

Testing Scenario: Select the Gender (Female) radio button Steps:

Code: public static void main (String [] args) throws InterruptedException {

  System.setProperty("webdriver.chrome.driver", "C:\\chromedriver_win32\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); // Step 1 - Launch Browser driver.get("http://toolsqa.com/automation-practice-form"); // Step 2 - Open Test URL List<WebElement> rdBtn_Sex = driver.findElements(By.name("sex")); // int size = rdBtn_Sex.size(); System.out.println("Total no of radio button :"+size); for (int i=0; i< size; i++) { String sValue = rdBtn_Sex.get(i).getAttribute("value"); // Step 3 - 3. Select the Radio button (female) by Value 'Female' System.out.println("Radio button Name "+sValue); if (sValue.equalsIgnoreCase("Female")) { rdBtn_Sex.get(i).click(); } } 
0
source share

What you should do all the time is to provide a locator for each object on the page, and then use the method.

how

driver.findElement(By.xpath(//CLASS[contains(., 'what you are looking for')])).click();

-one
source share

All Articles