Java编程语言下Selenium 对于下拉框,单选,多选等选择器的操作
1 WebElement selector = driver.findElement(By.id("Selector")); 2 Select select = new Select(selector); 3 select.selectByIndex(3); 4 select.selectByVisibleText("桃子"); 5 select.selectByValue("apple"); 6 7 System.out.println(select.getAllSelectedOptions().toString()); 8 Thread.sleep(10000); 9 WebElement showSelectResult = driver.findElement(By.name("showSelectResult")); 10 showSelectResult.click(); 11 Thread.sleep(10000); 12 Alert yourSelect = driver.switchTo().alert(); 13 Assert.assertTrue(yourSelect.getText().contains("苹果")); 14 Thread.sleep(10000); 15 yourSelect.accept(); 16 17 // for radio Box 18 19 WebElement duRadioBox = driver.findElement(By.cssSelector("div#radio>input.Baidu")); 20 WebElement aLiRadioBox = driver.findElement(By.cssSelector("div#radio>input.Alibaba")); 21 WebElement TXRadioBox = driver.findElement(By.cssSelector("div#radio>input.Tencent")); 22 WebElement MiRadioBox = driver.findElement(By.cssSelector("div#radio>input.Mi")); 23 Thread.sleep(10000); 24 Assert.assertTrue(TXRadioBox.isSelected()); 25 Assert.assertTrue(!MiRadioBox.isEnabled()); 26 Thread.sleep(10000); 27 28 try { 29 30 if (duRadioBox.isEnabled()) { 31 duRadioBox.click(); 32 System.out.println("元素被点击"); 33 } 34 } catch (Exception e) { 35 36 System.out.println("点击元素失败。" + e.toString()); 37 } 38 39 Assert.assertTrue(duRadioBox.isSelected()); 40 System.out.println(duRadioBox.isSelected()); 41 Thread.sleep(10000); 42 43 aLiRadioBox.click(); 44 Assert.assertTrue(aLiRadioBox.isSelected()); 45 Thread.sleep(10000); 46 47 // for checkBox 48 49 List<WebElement> webCheckBox = driver.findElements(By.xpath("//input[@type='checkbox']")); 50 51 for (WebElement e : webCheckBox) { 52 e.click(); 53 Assert.assertTrue(e.isSelected()); 54 Thread.sleep(10000); 55 }
------------------------- A little Progress a day makes you a big success... ----------------------------