Java+Selenium3方法篇24-单选和多选按钮操作

来源:互联网 发布:java 1.8 下载地址 编辑:程序博客网 时间:2024/06/14 19:45

本篇介绍webdriver处理前端单选按钮的操作。单选按钮一般叫raido button,就像我们在电子版的单选答题过程一样,单选只能点击一次,如果点击其他的单选,之前单选被选中状态就会变成未选中。单选按钮的点击,一样是使用click方法。下面我们介绍百度新闻首页中有两个单选按钮,我们根据for选好,依次点击第一个,然后点击第二个,由于第一个是默认选中状态,所以看不到第一个点击的效果。

相关脚本代码如下。

[java] view plain copy
  1. package lessons;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.concurrent.TimeUnit;  
  5.   
  6. import org.openqa.selenium.By;  
  7. import org.openqa.selenium.WebDriver;  
  8. import org.openqa.selenium.WebElement;  
  9. import org.openqa.selenium.chrome.ChromeDriver;  
  10.   
  11. public class ElementOpration {  
  12.     public static void main(String[] args) throws Exception {    
  13.           
  14.         System.setProperty("webdriver.chrome.driver"".\\Tools\\chromedriver.exe");    
  15.              
  16.         WebDriver driver = new ChromeDriver();    
  17.        
  18.         driver.manage().window().maximize();    
  19.          
  20.         driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);  
  21.             
  22.         driver.get("http://news.baidu.com");    
  23.          
  24.         Thread.sleep(1000);  
  25.           
  26.         // 把单选按钮放在一个list里面  
  27.         ArrayList<WebElement> search_type = (ArrayList<WebElement>) driver.findElements(By.xpath("//*/p[@class='search-radios']/input"));  
  28.           
  29.         for(WebElement ele : search_type){  
  30.             ele.click();  
  31.             Thread.sleep(1000);  
  32.         }  
  33.           
  34.     }    
  35. }  

       上面介绍了单选按钮是采用click方法,其实多选按钮一样是采用click,我们这里打开一个问卷调查,利用for循环去一次点击每个题目的单选和多选按钮。自己运行下观察效果。

[java] view plain copy
  1. package lessons;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.concurrent.TimeUnit;  
  5.   
  6. import org.openqa.selenium.By;  
  7. import org.openqa.selenium.WebDriver;  
  8. import org.openqa.selenium.WebElement;  
  9. import org.openqa.selenium.chrome.ChromeDriver;  
  10.   
  11. public class ElementOpration {  
  12.     public static void main(String[] args) throws Exception {    
  13.           
  14.         System.setProperty("webdriver.chrome.driver"".\\Tools\\chromedriver.exe");    
  15.              
  16.         WebDriver driver = new ChromeDriver();    
  17.        
  18.         driver.manage().window().maximize();    
  19.          
  20.         driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);  
  21.             
  22.         driver.get("https://www.sojump.com/m/2792226.aspx/");    
  23.          
  24.         Thread.sleep(1000);  
  25.           
  26.         // 把单选按钮放在一个list里面  
  27.         ArrayList<WebElement> answer_options = (ArrayList<WebElement>) driver.findElements(By.xpath("//*/div[@id='divQuestion']/fieldset/div/div/div/span/input/../a"));  
  28.           
  29.         for(WebElement ele : answer_options){  
  30.             ele.click();  
  31.             Thread.sleep(1000);  
  32.         }  
  33.           
  34.     }    
  35. }  
       上面介绍了一组元素采用for循环遍历操作的方法,如果是单个单选按钮或者单个多选按钮,需要用到根据单选按钮旁边的文字去定位该单选按钮,所以这里需要用到Xpath的相对路径写法。其实上面第二个例子input/../a就是采取了相对路径写法,才能看到点击选中的效果。
阅读全文
0 0
原创粉丝点击