2011-04-28 74 views
8

我在做自动化测试使用硒,我需要关于如何选择单选按钮的帮助。如果可能的话,请帮我用硒代码。硒代码选择单选按钮

+0

怎么回答你推广Tnem工作提供?如果有,请小心接受? – 2012-12-10 12:41:40

回答

7

假设你有硒设置它只是:

selenium.click('radio button locator'); 

你可能想看看硒的javadoc http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/java/com/thoughtworks/selenium/Selenium.html

+4

提到的Selenium doc页面是一个好的开始,但是对于定位器的更全面的参考,请参阅我的挂图:[XPath,CSS,DOM和Selenium:Rosetta Stone和Cookbook](http://www.simple-talk.com /dotnet/.net-framework/xpath,-css,-dom-and-selenium-the-rosetta-stone/) – 2011-04-29 17:22:27

0
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()); 

     } 
} 

在我与选择单选按钮,上面的例子中“往返“使用”JavaScript“。

最后四行用于验证页面中是否选择了预期的单选按钮。

注:我提供了简单易用的解决方案来解决选定网页中的问题(选择收音机)。可以编写更好的代码。 (用户可以编写一个方法来接受无线电ID并循环所有现有的单选按钮来查看其中哪一个被选中)。

+0

如果用户无法选择单选按钮并通过“driver.findElement (““) “ 方法。我知道少数情况下,“driver.findElement(”“)”及其操作无法完成预期的工作(既不选择也不验证)。 JavaScriptExecutor是任何测试者的第二选择。 – MKod 2013-02-06 17:12:12

1
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] 

等等 使用这个命令来随机选择任何单选按钮

0

我用这个方法:

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

你可以做的是这样的:

创建一个带有WebElement返回类型的方法,并使用Method findElement()。示例:

WebDriver test; 
test = new FirefoxDriver(); 

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

存储WebElement并使用方法click()。例如:

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

希望这有助于!

-1

你应该一直在做的是为页面中的每个对象提供一个定位器,然后使用一个方法。

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

0

测试场景:选择性别(女)单选按钮 步骤:

  1. 启动新的浏览器
  2. 打开URL http://toolsqa.wpengine.com/automation-practice-form/

  3. 选择收音机按钮(女)的价值“女”

代码: 公共静态无效的主要(字串[] args)抛出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(); 

     } 
    }