package web;
import java.util.List;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class SelectsStudyTest extends TestCase{
WebDriver dr;
@Before
public void setUP() throws InterruptedException{
//System.setProperty("webdriver.firefox.bin","D:/Program Files/Mozilla Firefox/firefox.exe" );
dr = new FirefoxDriver();
dr.get("http://jiaoyou.51.com/chaoy");
Thread.sleep(3000);
}
@Test
public void testPi() throws InterruptedException {
dr = new FirefoxDriver();
dr.get("http://jiaoyou.51.com/chaoy");
Thread.sleep(3000);
/**
linktext 方式实现
dr.findElement(By.linkText("点击打开")).click();
dr.findElement(By.linkText("2")).click();
dr.findElement(By.xpath("(//a[contains(text(),'点击打开')])[2]")).click();
dr.findElement(By.xpath("(//a[contains(text(),'5')])[20]")).click();
dr.findElement(By.id("search_btn")).click();
*/
/**
cssselector方式实现
dr.findElements(By.cssSelector(".arrow")).get(0).click();
dr.findElements(By.cssSelector(".arrow")).get(1).click();
*/
/**select 方式实现,但是没调试成功
Select selectage1 = new Select(dr.findElement(By.xpath("(//a[contains(text(),'点击打开')])[2]")));
Select selectage2 = new Select(dr.findElement(By.xpath("(//a[contains(text(),'5')])[20]")));
selectage1.selectByValue("1");
selectage2.selectByValue("5");
*/
//Xpath方式实现
dr.findElement(By.xpath("//span[text()='年龄:']/parent::*/div[1]//a[text()='点击打开']")).click();
dr.findElement(By.xpath("//span[text()='年龄:']/parent::*/div[1]/ul//li[30]/a")).click();
dr.findElement(By.xpath("//span[text()='年龄:']/parent::*/div[2]//a")).click();
dr.findElement(By.xpath("//span[text()='年龄:']/parent::*/div[2]/ul//li[40]/a")).click();
dr.findElement(By.xpath("//span[text()='地区:']/parent::*/div[2]/div/a[text()='点击打开']")).click();
dr.findElement(By.xpath("//span[text()='地区:']/parent::*/div[1]/ul/li[1]//span")).click();
dr.findElement(By.xpath("//span[text()='地区:']/parent::*/div[1]/ul[2]/li[1]//span")).click();
dr.findElement(By.id("search_btn")).click();
//dr.findElement(By.xpath("//a[text()='查找']")).click();
Thread.sleep(3000);
//验证查询结果:
By cssSelector = By.cssSelector(".location");
List<WebElement> findElements = dr.findElements(cssSelector);
for(WebElement findElement:findElements){
System.out.println(findElement.getText());
assertEquals("定位到地址不为合肥的元素","合肥",findElement.getText());
//assertEquals(findElement.getText(),"合肥");
}
dr.quit();
}
}
<img src="https://img-blog.csdn.net/20140807111626411?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbnhuMDAx/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />