Selenium Web 自动化 - Selenium常用API

2025最新selenium教程,python网页自动化测试脚本,pytest+selenium自动化测试实战速成

1 对浏览器操作

1.1 用webdriver打开一个浏览器

//打开firefox浏览器:
WebDriver driver = new FirefoxDriver(); 
//打开IE浏览器
WebDriver driver = new InternetExplorerDriver ();
//打开HtmlUnit浏览器
WebDriverdriver = new HtmlUnitDriver(); 
//打开chrome浏览器
WebDriverdriver = new ChromeDriver();

1.2 最大化浏览器&关闭浏览器

WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.close();
driver.quit();

1.3 设置浏览器窗口大小

1.4 打开测试页面

打开测试页面
driver.get("http://www.baidu.com/");
driver.navigate().to("http://www.baidu.com/"); 
//navigate方法会产生1个Navigator对象,其封装了与导航相关的一些方法,比如前进后退等

1.5 处理浏览器弹出的新窗口

2 页面元素定位

Webdriver提供下面两种方法来定位页面元素,参数是By对像,最常用是By.id和By.name查找。

  • findElement   定位某个元素,如果没有找到元素会抛出异常:NoSuchElementException
  • findElements     定位一组元素

例如需要定位如下元素: 

  <input class="input_class" type="text" name="passwd" id="passwd-id" /> 

//By.id
WebElement element = driver.findElement(By.id("passwd-id"));
//By.name
WebElement element = driver.findElement(By.name("passwd"));
//By.xpath
WebElement element =driver.findElement(By.xpath("//input[@id='passwd-id']")); 
//By.className
WebElement element = driver.findElement(By.className("input_class"));
//By.cssSelector
WebElement element = driver.findElement(By.cssSelector(".input_class"));
//By.linkText
//通俗点就是精确查询
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com/"); 
WebElement element = driver.findElement(By.linkText("百科"));
//By.partialLinkText:
//这个方法就是模糊查询
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com/"); 
WebElement element = driver.findElement(By.partialLinkText("hao"));
//By.tagName
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com/"); 
String test= driver.findElement(By.tagName("form")).getAttribute("name");
System.out.println(test);

3 如何对页面元素进行操作

3.1 WebElement相关方法

Method   Summary
void clear()If   this element is a text entry element, this will clear the value.
void click()Click   this element.
WebElement findElement(By by)Find   the first WebElement  using the given method.
 java.util.List<WebElement> findElement(By   by)Find   all elements within the current context using the given mechanism.
 java.lang.String getAttribute(java.lang.String   name)Get   the value of a the given attribute of the element.
 java.lang.String getCssValue(java.lang.String.propertyName)Get   the value of a given CSS property.
Point GetLocation()Where   on the page is the top left-hand corner of the rendered element?
 Dimension getSize()What   is the width and height of the rendered element?
 java.lang.String getTagName() Get   the tag name of this element.
 java.lang.String getText() Get   the visible (i.e. not hidden by CSS) innerText of this element, including
sub-elements, without any leading or trailing whitespace.
 boolean isDisplayed()Is   this element displayed or not? This method avoids the problem of having to   parse an element's "style" attribute.
 boolean isEnabled()Is the element currently enabled or not? This will generally return true for everything but disabled input elements.
 boolean isSelected()Determine   whether or not this element is selected or not.
 void sendKeys(java.lang.CharSequence...   keysToSend)Use   this method to simulate typing into an element, which may set its value.
 void submit()If   this current element is a form, or an element within a form, then this will   be submitted to the remote server.

3.2 iFrame的处理

driver.switchTo().frame(“city_set_ifr”); //传入的是iframe的ID
dr.switchTo().defaultContent(); //如果要返回到以前的默认content

3.3 输入框(text field or textarea)

WebElement element = driver.findElement(By.id("passwd-id"));
element.sendKeys(“test”);//在输入框中输入内容:
element.clear();       //将输入框清空
element.getText();     //获取输入框的文本内容:

3.4 下拉选择框(Select)

Select select = new Select(driver.findElement(By.id("select")));  
select.selectByVisibleText(“A”);
select.selectByValue(“1”); 
select.deselectAll();
select.deselectByValue(“1”);
select.deselectByVisibleText(“A”);
select.getAllSelectedOptions();
select.getFirstSelectedOption(); 

3.5 单选项(Radio Button)

WebElement radio=driver.findElement(By.id("BookMode"));
radio.click();       //选择某个单选项
radio.clear();      //清空某个单选项
radio.isSelected();  //判断某个单选项是否已经被选择

3.6 多选项(checkbox)

WebElement checkbox = driver.findElement(By.id("myCheckbox."));
checkbox.click();
checkbox.clear();
checkbox.isSelected();
checkbox.isEnabled();

3.7 按钮(button)

WebElement btn= driver.findElement(By.id("save"));
btn.click();      //点击按钮
btn.isEnabled ();  //判断按钮是否enable

3.8 处理Alert

弹出对话框(Popup dialogs)

Alert alert = driver.switchTo().alert();
alert.accept();  //确定
alert.dismiss();  //取消
alert.getText(); //获取文本

3.9 上传文件

3.9.1 元素标签是Input时上传方式

Upload.html文件内容如下:

<body>
<input type="file" id="fileControl" value="选择文件"/>
</body>
3.9.2 通过操作桌面浏览窗口上传

3.10 Selenium处理HTML5

3.10.1 处理Vedio

这就需要了解html5中vedio的相关方法了,可以参考HTML Audio/Video DOM 参考手册

3.10.2 处理Canvas 

3.11 表单(Form)

//Form中的元素的操作和其它的元素操作一样,对元素操作完成后对表单的提交可以:
WebElement approve = driver.findElement(By.id("approve"));
approve.click();
//或
approve.submit();//只适合于表单的提交

4 其他

 

4.1 等待元素加载

超时设置

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);      //识别元素时的超时时间
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);  //页面加载时的超时时间
driver.manage().timeouts().setScriptTimeout(10, TimeUnit.SECONDS);  //异步脚本的超时时间
  • 硬性等待  Thread.sleep(int sleeptime);
  • 智能等待
  • 设置等待页面加载完毕

4.2 执行JS脚本

selenium常用的js总结

有时候我们需要JS脚本来辅助我们进行测试,比如我们用JS赋值或者用js执行点击操作等。执行JS脚本比较适用某些元素不易点击的情况下使用,比如网页内容太长,当前窗口太长,想要点击那些不在当前窗口可以看到元素可以用此方法。 

4.3 模拟键盘操作

有时候有些元素不便点击或者做其他的操作,这个时候可以借助selenium提供的Actions类,它可以模拟鼠标和键盘的一些操作,比如点击鼠标右键,左键,移动鼠标等操作。对于这些操作,使用perform()方法进行执行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值