(二)简单元素操作、方法、鼠标操作

WebElement findElement(By by):定位元素用webelement,返回的是webelement;对元素的操作依然是webelement

在WebDriver中,大多简单有效的页面交互的方法都是通过WebElement接口提供,最常用的操作页面元素方法有如下几个:

clear() --清除文本,如果是一个文本框

sendKeys(*value)--在元素上模拟键盘输入

click() -- 单击元素

	System.setProperty("webdriver.firefox.bin", "D:/Program Files (x86)/Mozilla Firefox/firefox.exe");
	System.setProperty("webdriver.gecko.driver", "E://selenium//geckodriver-v0.24.0-win64//geckodriver.exe");
	
	WebDriver driver=new FirefoxDriver();
	driver.get("http://www.baidu.com/");//向浏览器发送网址
	
	WebElement txtbox=driver.findElement(By.id("kw")); //根据属性id找到百度输入框
	CharSequence[] str={"selenium java"};
	txtbox.sendKeys(str);//文本框输入内容
	
	txtbox.clear();
	
	CharSequence[] str1={"selenium python"};
	txtbox.sendKeys(str1);
	
	WebElement btn=driver.findElement(By.id("su")); //根据属性id=su找到百度一下搜索按钮
	btn.click();//点击
	

CSDN新建文章为草稿

                    driver.get("https://mp.csdn.net/");
		    driver.findElement(By.linkText("富文本编辑器")).click();
		    driver.findElement(By.id("txtTitle")).click();
		    driver.findElement(By.id("txtTitle")).clear();
		    driver.findElement(By.id("txtTitle")).sendKeys("(一)草稿");
		    
		    driver.findElement(By.id("7090333")).click();
		    driver.findElement(By.id("7173582")).click();//复选框
		    driver.findElement(By.id("selType")).click();
		    new Select(driver.findElement(By.id("selType"))).selectByVisibleText("原创");//select下拉菜单
		    driver.findElement(By.id("radChl")).click();
		    new Select(driver.findElement(By.id("radChl"))).selectByVisibleText("编程语言");
		    driver.findElement(By.id("btnDraft")).click();
		    driver.findElement(By.linkText("文章管理")).click();
		    
		    driver.findElement(By.linkText("(一)草稿")).click();
		    

二、常用方法

submit():用于提交表单,这里特别用于没有提交按钮的情况,例如搜索框输入关键字之后的“回车”操作,就可以通过submit()来提交搜索框的内容,与上面找到按钮进行对比

WebDriver driver=new FirefoxDriver();
	driver.get("http://www.baidu.com/");//向浏览器发送网址
	
	WebElement txtbox=driver.findElement(By.id("kw")); //根据属性id找到百度输入框
	CharSequence[] str={"selenium java"};
	txtbox.sendKeys(str);//文本框输入内容
	txtbox.submit();

getSize() :返回元素的尺寸

getAttribute(name):获得属性值

isDisplayed():设置该元素是否用户可见

WebDriver driver=new FirefoxDriver();
	driver.get("http://www.baidu.com/");//向浏览器发送网址
	WebElement txtbox=driver.findElement(By.id("kw"));
	System.out.println(txtbox.getAttribute("maxlength")); //获取元素的属性
	System.out.println(txtbox.getSize()); //返回元素的尺寸
	System.out.println(txtbox.isDisplayed());

三、鼠标操作

import org.openqa.selenium.interactions.Actions;

Actions类    右击、双击、悬停、指针拖动等操作

contextClick():右击

clickAndHold():鼠标悬停

move_to_element():鼠标悬停

doubleClick():双击

release():释放鼠标

perform():执行所有Actions中存储的行为

WebDriver driver=new FirefoxDriver();
	driver.get("http://www.baidu.com/");
	Actions action=new Actions(driver);
	
	//hold鼠标
	//clickAndHold:Clicks (without releasing) at the current mouse location. 
	action.clickAndHold(driver.findElement(By.name("tj_briicon"))).perform();
	
	Thread.sleep(10000);
	//释放鼠标
	action.release(driver.findElement(By.name("tj_briicon"))).perform();
	
	/*
	WebDriver driver=new FirefoxDriver();
	driver.get("http://www.baidu.com/");
	Actions action=new Actions(driver);
	
	//鼠标右键点击指定的元素
	
	//perform:执行所有Actions中存储的行为,可以理解为对整个操作事件的提交操作
	//action.contextClick(driver.findElement(By.id("su"))).perform(); 
	
	//clickAndHold:Clicks (without releasing) at the current mouse location. 
	//action.clickAndHold(driver.findElement(By.name("tj_briicon"))).perform();
	
	//鼠标移动 因为悬浮操作,和上面的效果一致
	action.moveToElement(driver.findElement(By.name("tj_briicon"))).perform();
	

若要操作悬浮下拉菜单中选项,则要先move_to_element()再操作;否则,提示unable to locate xxxx

 @Test  //鼠标操作
	  public void testUntitledTestCase() throws Exception {
		    driver.get("https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc&fs=北京,TSP&ts=呼和浩特,HHC&date=2019-06-24&flag=N,N,Y");
		   
		 	Actions action=new Actions(driver);
		 	
		 	
		 //点击并保持
		 	WebElement element=driver.findElement(By.linkText("我的12306"));
		 	action.moveToElement(element).perform(); 
		 
			WebElement element1=driver.findElement(By.linkText("温馨服务查询"));
		
			
			action.click(element1).perform(); 
				
		 	
	 }

报错:WebDriverException: Message: TypeError: rect is undefined 

原因:存在两个相同符合条件的元素

posted @ 2019-04-30 14:15  测试开发分享站  阅读(135)  评论(0编辑  收藏  举报