Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
selenium代码选择单选按钮_Selenium - Fatal编程技术网

selenium代码选择单选按钮

selenium代码选择单选按钮,selenium,Selenium,我正在使用selenium进行自动化测试,我需要有关如何选择单选按钮的帮助。如果可能,请帮助我使用selenium java代码。假设您已经设置了selenium,只需: selenium.click('radio button locator'); 您可能想看看selenium javadoc 在上面的示例中,我使用“JavaScript”选择带有“往返”的单选按钮 最后四行用于验证并查看是否在页面中选择了预期的单选按钮 注意:我在choosen网页中给出了解决问题的简单解决方案(选择收音机

我正在使用selenium进行自动化测试,我需要有关如何选择单选按钮的帮助。如果可能,请帮助我使用selenium java代码。

假设您已经设置了selenium,只需:

selenium.click('radio button locator');
您可能想看看selenium javadoc

在上面的示例中,我使用“JavaScript”选择带有“往返”的单选按钮

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

注意:我在choosen网页中给出了解决问题的简单解决方案(选择收音机)。可以编写更好的代码。(用户可以编写一个方法来接受单选ID,并循环遍历所有现有单选按钮,以查看选择了哪一个)

等等。。。 使用此命令选择随机和任意单选按钮

我使用此方法:

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

你能做的是:

创建具有WebElement返回类型的方法,并使用方法findElement()。例如:

WebDriver test;
test = new FirefoxDriver();

public static WebElement checkAndGet(By b) throws Exception {
    return test.findElement(b);
    }
WebElement radiobutton = checkAndGet(By.xpath("//span[@class='label ng-binding']");
radiobutton.click();
存储WebElement并使用方法click()。例如:

WebDriver test;
test = new FirefoxDriver();

public static WebElement checkAndGet(By b) throws Exception {
    return test.findElement(b);
    }
WebElement radiobutton = checkAndGet(By.xpath("//span[@class='label ng-binding']");
radiobutton.click();

希望这有帮助

您应该始终为页面中的每个对象提供一个定位器,然后使用一种方法


driver.findElement(By.xpath(//CLASS[contains(,'what your'seaking')))。单击()

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

  • 启动新浏览器
  • 打开URL

  • 按“女性”值选择单选按钮(女性)

  • 代码: 公共静态void main(字符串[]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();
    
            }
        }
    
    System.setProperty(“webdriver.chrome.driver”,“C:\\chromedriver\u win32\\chromedriver.exe”);
    WebDriver driver=new ChromeDriver();//步骤1-启动浏览器
    驱动程序。获取(“http://toolsqa.com/automation-practice-form“”;//步骤2-打开测试URL
    List rdBtn_Sex=driver.findElements(By.name(“Sex”);//
    int size=rdBtn_Sex.size();
    System.out.println(“单选按钮总数:“+尺寸”);
    对于(int i=0;i
    提到的Selenium doc页面是一个很好的开始,但有关定位器的更全面的参考,请参见我的挂图:Tnem提供的答案对您有何作用?如果答案正确,请接受。如果用户无法选择单选按钮并在“driver.findElement”(“”)的帮助下进行验证,则上述解决方案效果更好我知道很少有情况下“driver.findelelement(“”)及其操作无法完成预期的工作(既不选择也不验证)。JavaScriptExecutor是任何测试人员的第二选择。