简体   繁体   中英

how to select radio button in selenium webdriver by not using x path

The page fragment with the radio button I want to access:

<td style="font-size:xx-small"></td>
<td style="font-size:xx-small">
    <input type="radio" value="EMPLOYEE" name="user_type"></input>

    EMPLOYEE

    <input type="radio" checked="checked" value="CUSTOMER" name="user_type"></input>

    CUSTOMER

</td>

And below the Java code to test the user interaction with the radio button:

package com.ej.zob.modules;

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.Select;

public class Login {
    public void Execute(String UserName,String Password,String Language,String Type)
    {
      LaunchApplication.driver.findElement(By.id("uname")).sendKeys(UserName);
      LaunchApplication.driver.findElement(By.id("pwd")).sendKeys(Password);

      Select sel = new Select(LaunchApplication.driver.findElement(By.name("language")));
      sel.selectByVisibleText(Language);
      //LaunchApplication.driver.findElement(By.name("language")).sendKeys(Language);
      String val=LaunchApplication.driver.findElements(By.name("user_type")).get(0).getText();
      //String value =LaunchApplication.driver.findElements(By.name("user_type")).get(1).getText();
      if(val.trim().equals("EMPLOYEE")){
        LaunchApplication.driver.findElement(By.name("EMPLOYEE")).click();          
      }
      LaunchApplication.driver.findElement(By.name("imageField")).click();      
    }
}

Try this:

List<WebElement> list = driver.findElements(By.name("user_type"));
Iterator<WebElement> i = list.iterator();
while(i.hasNext()) 
{
WebElement rdButton = i.next();    
if(rdButton.getAttribute("value").contains("employee"))
{
   //select this radio button
   rdButton.click();
}
} 

a google of 'java selenium find element by' suggests:

  • driver.findElement(By.tagName("element html tag name"))
  • driver.findElement(By.name("element name"))
  • driver.findElement(By.id("element id"))

etc..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM