当前位置: 首页 > 工具软件 > radio4000 > 使用案例 >

Java + Selenium(四) 常见的元素处理-radio

王棋
2023-12-01

单选框

  • click
  • clear
  • isSelected
package com.test.selenium;

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ActionSelenium {
	public WebDriver driver;
	
	public void InitDriver(){
		System.setProperty("webdriver.firefox.bin","C:\\Program Files\\Mozilla Firefox\\firefox.exe");
		driver = new FirefoxDriver();
		driver.navigate().to("https://www.imooc.com/user/newlogin");
		driver.manage().window().maximize();
	}
	public void inputBox(){
		driver.findElement(By.name("email")).sendKeys("895236983@qq.com");
		try{
			Thread.sleep(2000);
		}catch(InterruptedException e){
			e.printStackTrace();
		}
		driver.findElement(By.name("email")).clear();
		String s = driver.findElement(By.name("email")).getAttribute("placeholder");
		System.out.println(s);
		driver.findElement(By.name("email")).sendKeys("895236983@qq.com");
		driver.findElement(By.name("password")).sendKeys("sss");
		driver.findElement(By.className("moco-btn-red")).click();
		try{
			Thread.sleep(4000);
		}catch(InterruptedException e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 单选框
	 * 
	 * */
	public void radioBox(){
		driver.get("https://www.imooc.com/user/setprofile");
		driver.findElement(By.className("pull-right")).click();
		//driver.findElement(By.xpath(".//*[@id='profile']/div[4]/div/label[1]")).click();
		List<WebElement> elements = driver.findElements(By.xpath(".//*[@id='profile']/div[4]/div/label//input"));
		System.out.println(elements.size());
		try{
			Thread.sleep(2000);
		}catch(InterruptedException e){
			e.printStackTrace();
		}
		for(WebElement radio:elements){
			boolean flag = radio.isSelected();
			if(flag==false){
				radio.click();
				break;
			}else{
				System.out.println("选中了");
			}
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ActionSelenium as = new ActionSelenium();
		as.InitDriver();
		as.inputBox();
		as.radioBox();
	}
}
 类似资料: