Selenium2学习笔记一

Selenium1和Selenium2

    Selenium1.0是一个基于浏览器的开源自动化测试工具,它中可以使用多种语言编程,在运行Selenium1.0程序之前,需要先启动selenium server服务,及selenium remote control,简称为RC。由于其实由JavaScript实现的,所以只要支持JavaScript的浏览器都可以很好的支持它。而Selenium2.0是Selenium1.0和WebDriver的结合,WebDriver可以直接操作HTML Unit驱动,速度快。Selenium2.0最显著的特点就是不需要再启动server服务了。

Selenium2下载

    选择其中一个版本,如下图所示:

    selenium-java.zip是用于我们在Java编程中使用的,里面包含了所需的jar包,在Java编程中需要加入里面相应的jar包方可使用selenium提供的Java API。IEDriverServer.zip是一个IE浏览器的驱动,当我们在程序中使用InternetExplorerDriver来创建一个IE浏览器的WebDriver时,需要使用到。selenium-server.zip和selenium-server-standalone.jar两个需要配合使用,用于启动selenium服务。

启动Selenium2 Server

  1. 首先下载JDK,并配置到环境变量中。
  2. 下载selenium-server-2.50.0.zip并解压出selenium-server-2.50.0文件夹,下载selenium-server-standalone-2.50.0.jar,将其放入selenium-server.zip解压的目录下面。在Windows下打开命令行,来到selenium-server-2.50.0文件夹下,输入以下命令:
java -jar selenium-server-standalone-2.50.0.jar
      可以看到,Selenium Server已经启动成功了,可以看到连接的IP地址和端口号。
      

启动firefox浏览器

    首先需要在IDE下面创建一个Java 项目,然后加入selenium-java-2.50.0.zip解压出的文件夹下的jar包,可以全部加入,并添加到class path下面。 以下程序大概做的是:创建一个WebDriver去访问百度,然后根据id得到搜索输入框,输入Selenium2,点击百度一下按钮进行查询。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FirefoxExample {
	public static void main(String[] args) {
		//设置火狐浏览器.exe执行文件所在的位置,WebDriver创建时会到该目录下去寻找
		System.setProperty("webdriver.firefox.bin","D:\\Firefox\\firefox.exe");
		WebDriver driver = new FirefoxDriver();
		
		Navigation navigation = driver.navigate();
		navigation.to("https://www.baidu.com");
		
		WebElement subox = driver.findElement(By.id("kw"));
		subox.sendKeys("Selenium2");
		WebElement subtn = driver.findElement(By.id("su"));
		subtn.click();

		try {
		<span style="white-space:pre">	</span>Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		driver.close();
	}
}
    如果运行时出现了如下类似的异常,可能的原因是你当前使用的selenium-java-2.50.0版本,不支持你当前使用的火狐浏览器,可以下载新版本的selenium-java-2.50.0或者下载版本较低的火狐浏览器。
org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
modules/Task.jsm:319:40
TaskImpl@resource://gre/modules/Task.jsm:280:3
createAsyncFunction/asyncFunction@resource://gre/modules/Task.jsm:254:14
loadManifestFromZipFile<@resource://gre/modules/addons/XPIProvider.jsm:1406:26
TaskImpl_run@resource://gre/modules/Task.jsm:319:40
TaskImpl@resource://gre/modules/Task.jsm:280:3
createAsyncFunction/asyncFunction@resource://gre/modules/Task.jsm:254:14
loadManifestFromFile@resource://gre/modules/addons/XPIProvider.jsm:1416:12
AddonInstaller.InstallerService._shouldInstall/<@resource://addoninstaller/installerService.js:319:49
safeCall@resource://gre/modules/AddonManager.jsm:179:5
makeSafe/<@resource://gre/modules/AddonManager.jsm:195:25
Handler.prototype.process@resource://gre/modules/Promise.jsm -> resource://gre/modules/Promise-backend.js:937:23
this.PromiseWalker.walkerLoop@resource://gre/modules/Promise.jsm -> resource://gre/modules/Promise-backend.js:816:7
this.PromiseWalker.scheduleWalkerLoop/<@resource://gre/modules/Promise.jsm -> resource://gre/modules/Promise-backend.js:750:11

启动IE浏览器

    IE浏览器与火狐不同的是,需要下载IE浏览器的驱动,并设置到系统变量中才可以运行。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class IEExample {
	public static void main(String[] args) {
		//设置IE浏览器驱动的位置
		System.setProperty("webdriver.ie.driver", "E:\\OpenSource\\selenium\\IEDriverServer_x64_2.50.0\\IEDriverServer.exe");
		WebDriver driver = new InternetExplorerDriver();
		
		Navigation navigation = driver.navigate();
		navigation.to("https://www.baidu.com");
		
		WebElement subox = driver.findElement(By.id("kw"));
		subox.sendKeys("软件测试");
		WebElement subtn = driver.findElement(By.id("su"));
		subtn.click();

		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		driver.close();
	}
}

启动chrome浏览器

    谷歌浏览器同样需要下载驱动,并设置到系统变量中。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.chrome.ChromeDriver;

public class ChromeExample {
	public static void main(String[] args) {
		//设置谷歌浏览器驱动的位置
		System.setProperty("webdriver.chrome.driver", "E:\\OpenSource\\selenium\\chromedriver_win32\\chromedriver.exe");
		System.setProperty("webdriver.chrome.bin", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
		WebDriver driver = new ChromeDriver();
		
		Navigation navigation = driver.navigate();
		navigation.to("https://www.baidu.com");
		
		WebElement subox = driver.findElement(By.id("kw"));
		subox.sendKeys("Selenium2");
		WebElement subtn = driver.findElement(By.id("su"));
		subtn.click();

		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		driver.close();
	}
}



当然,我可以帮你输出一篇Selenium学习笔记。以下是一份简单的学习笔记供参考: Selenium学习笔记 Selenium是一款用于自动化Web应用程序测试的强大工具。它提供了一组丰富的API,可以与各种编程语言(如Python、Java、C#等)结合使用,以实现Web应用程序的自动化测试。 1. 安装和配置 首先,你需要安装Selenium库。如果你使用Python,可以使用以下命令安装: ``` pip install selenium ``` 安装完成后,你还需要下载并配置相应的浏览器驱动程序。Selenium支持各种浏览器,如Chrome、Firefox、Safari等。你可以在Selenium官方网站上找到相应的驱动程序下载链接。 2. 基本操作 Selenium提供了一系列方法来模拟用户在浏览器中的操作,如打开URL、点击元素、输入文本等。以下是一些常用的基本操作示例: - 打开浏览器并访问URL: ```python from selenium import webdriver driver = webdriver.Chrome() # 使用Chrome浏览器驱动 driver.get("https://www.example.com") # 打开指定URL ``` - 查找元素并操作: ```python element = driver.find_element_by_id("element_id") # 根据元素ID查找 element.click() # 点击元素 input_element = driver.find_element_by_name("input_name") # 根据元素名称查找 input_element.send_keys("Hello, World!") # 输入文本 ``` - 执行JavaScript代码: ```python driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") # 滚动到页面底部 ``` 3. 高级操作 Selenium还提供了一些高级操作,如处理弹窗、切换窗口、等待元素加载等。以下是一些常用的高级操作示例: - 处理弹窗: ```python alert = driver.switch_to.alert # 切换到弹窗 alert.accept() # 接受弹窗 confirm = driver.switch_to.alert # 切换到确认框 confirm.dismiss() # 取消确认框 ``` - 切换窗口: ```python window_handles = driver.window_handles # 获取所有窗口句柄 driver.switch_to.window(window_handles[-1]) # 切换到最后一个窗口 ``` - 等待元素加载: ```python from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "element_id")) ) ``` 4. 测试框架 Selenium还可以与测试框架(如Pytest、JUnit等)结合使用,以便更好地组织和管理测试用例。你可以使用测试框架提供的断言方法来验证测试结果。 ```python import pytest def test_example(): driver = webdriver.Chrome() driver.get("https://www.example.com") assert "Example Domain" in driver.title # 断言页面标题包含指定文本 driver.quit() ``` 以上是一份简单的Selenium学习笔记,希望对你有所帮助。当然,Selenium还有很多其他功能和用法,你可以进一步深入学习和探索。祝你学习愉快!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值