簡體   English   中英

如何通過不使用x路徑來選擇Selenium Webdriver中的單選按鈕

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

我要訪問的帶有單選按鈕的頁面片段:

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

在Java代碼下方,用於測試用戶與單選按鈕的交互:

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

嘗試這個:

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

“ java selenium find element by”的谷歌建議:

  • driver.findElement(By.tagName(“元素html標簽名稱”))
  • driver.findElement(By.name(“元素名稱”))
  • driver.findElement(By.id(“ element id”))

等等..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM