选择单选按钮的硒代码

发布于 2024-11-04 01:07:05 字数 78 浏览 0 评论 0原文

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

Am doing automation tesing using selenium, i need help in regarding how to select radio button.If possible help me with selenium java code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

他夏了夏天 2024-11-11 01:07:05

假设您已经设置了selenium:

selenium.click('radio button locator');

您可能需要查看selenium javadoc http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/java/com/thoughtworks/selenium/Selenium.html

Assuming you have selenium set up its just:

selenium.click('radio button locator');

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

神回复 2024-11-11 01:07:05
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 ...
使用此命令可以选择随机和任何单选按钮

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 this commands to select random and any radio button

累赘 2024-11-11 01:07:05
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”选择带有“ROUND TRIP”的单选按钮。

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

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

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 am selecting radio button with "ROUND TRIP" using "JavaScript".

The last four lines are to verify and see whether the expected radio button is selected in the page or not.

NOTE: I am giving simple easy solution to solve a problem (selecting a radio) in the choosen webpage. A better code can be written. (user can write a method to accept radio ID and loop through all the existing radio button to see which one of them is selected).

江南月 2024-11-11 01:07:05

我用这个方法:

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

I use this method:

String radioButtonId = "radioButtonId";
selenium.focus("id=" + radioButtonId);
selenium.click("id=" + radioButtonId, "concreteRadioButtonValue");
只有影子陪我不离不弃 2024-11-11 01:07:05

您可以执行以下操作:

创建一个具有 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();

希望这有帮助!

What you can do is this:

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

WebDriver test;
test = new FirefoxDriver();

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

Store the WebElement and use the Method click(). Example:

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

Hope this helps!

花开浅夏 2024-11-11 01:07:05

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

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

        }
    }

Test Scenario : Select Gender(Female) radio button
Steps:

  1. Launch new Browser
  2. Open URL http://toolsqa.wpengine.com/automation-practice-form/

  3. Select the Radio button (female) by Value ‘Female’

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

        }
    }
一萌ing 2024-11-11 01:07:05

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

就像

driver.findElement(By.xpath(//CLASS[contains(., '您要查找的内容')])).click();

What you should do all the time is provide a locator for every object within a page and then use a method.

Like

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文