最初にMavenプロジェクトを作成する
pom.xmlに追加します。これは、セレン用の最新のMaven依存パッケージです。
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0-alpha-5</version>
</dependency>
次に、3つのブラウザーに必要なセレンドライバーをダウンロードします。
私は、現在ダウンロードしているためにここにいる最新のドライバのすべてのことを提供し、ブラウザは最新版では、3種類を持つことができます必要なダウンロードするにはこちらをクリックしてください。
リンク:https ://pan.baidu.com/s/18M1Fya1ZJbEpy0SskI9nqQ抽出コード:0naa
ダウンロードしたくない場合は、ブラウザにこのセレンプラグインがある場合(GoogleとFirefoxのみ、IEにはプラグインがありません)、2番目の方法があります。また、Googleには、以下に示すように、Googleオンラインストアを開くためのアカウントも必要です。
まず、Chromeの実装コード
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Chrome_Browser {
@Test
public void f() throws InterruptedException {
//写代码操作浏览器,模拟人工测试:api
// System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");
// 1、 创建一个Chrome驱动
WebDriver driver = new ChromeDriver();
// 2、输入百度网站进行访问
driver.get("http://www.baidu.com");
// 3、找到百度输入框(元素定位,定位一个元素)
// 根据id去找 kw
WebElement input = driver.findElement(By.id("kw"));
// 说明确实找到这个输入框
System.out.println(input.getAttribute("maxlength"));
// 4、往输入框输入关键字
input.sendKeys("CSDN");
// 5、点击百度一下
WebElement searchButton = driver.findElement(By.id("su"));
searchButton.click();
//绝对路径
// driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/form/span[2]/input")).click();
//相对路径
driver.findElement(By.xpath("//*[@id=\"su\"]")).click();
// 获得输入框的value值
String actualkeywords = driver.findElement(By.id("kw")).getAttribute("value");
System.out.println("输入框的值:" + actualkeywords);
// 断言: 验证结果符合我的预期,符合就不打印;不符合,就把不同结果打印出来
Assert.assertEquals("CSDN", actualkeywords);
// 等待时间 3秒
Thread.sleep(3000);
//退出浏览器
driver.quit();
}
}
第二に、FireFox実装コード
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FireFox_Browser {
@Test
public void f() throws InterruptedException {
//写代码操作浏览器,模拟人工测试:api
// System.setProperty("webdriver.firefox.driver", "src/main/resources/geckodriver.exe");
// 1、firefox驱动
WebDriver driver = new FirefoxDriver();
// 2、输入百度网站进行访问
driver.get("http://www.baidu.com");
// 3、找到百度输入框(元素定位,定位一个元素)
// 根据id去找 kw
WebElement input = driver.findElement(By.id("kw"));
// 说明确实找到这个输入框
System.out.println(input.getAttribute("maxlength"));
// 4、往输入框输入关键字
input.sendKeys("CSDN");
// 5、点击百度一下
WebElement searchButton = driver.findElement(By.id("su"));
searchButton.click();
//绝对路径
// driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div/form/span[2]/input")).click();
//相对路径
driver.findElement(By.xpath("//*[@id=\"su\"]")).click();
// 获得输入框的value值
String actualkeywords = driver.findElement(By.id("kw")).getAttribute("value");
System.out.println("输入框的值:" + actualkeywords);
//// 验证结果符合我的预期,符合就不打印;不符合,就把不同结果打印出来
Assert.assertEquals("CSDN", actualkeywords);
// 等待时间 3秒
Thread.sleep(3000);
//退出浏览器
driver.quit();
}
}
3、IEブラウザー実装コード
これはより面倒なので、少し特別です。セキュリティの通知があります。すべてのウイルス対策ソフトウェアをオフにすることをお勧めします。そうしないと、情報を傍受するように求められる場合があります。
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
/**
* @author ZF
* @Description TODO
* @Date 2020/4/9 20:29
*/
public class IE_Browser {
@Test
public void test() throws InterruptedException {
// 1、驱动文件找不到的异常
System.setProperty("webdriver.ie.driver", "src/main/resources/IEDriverServer.exe");
// 创建一个对象,用来设置创建ie驱动时的各种设置
DesiredCapabilities capabilities = new DesiredCapabilities();
// 2、取消IE安全设置(忽略IE的Protected Mode的设置)
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
// 3、忽略浏览器的页面缩放设置
capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
// 4、设置一个初始化页面,防止window对象丢失
capabilities.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, true);
// IE驱动
WebDriver driver = new InternetExplorerDriver(capabilities);
// 2、输入百度网站进行访问
driver.get("http://www.baidu.com");
// 等待时间 3秒
Thread.sleep(3000);
driver.quit();
}
}
要約:
これらのコード、特にIEにアクセスする必要はありません。多くのブログでは、セキュリティ設定で保護モードを有効にするためのチェックボックスをオフにする必要があると述べています。実際、それらを削除する必要はありません。エクスペリエンス:すべての設定は不要で、360アンチウイルスをオフにするだけですマルウェアのプロンプトが表示されないようにするためです。チェックすると次の画像を開くことができ、すべてのブラウザに設定する環境変数はなく、必要もありません。ネットワーク全体で私の最も単純なブログに従ってください、それを使用できます。これには多くのコードとpythonが含まれています。該当します。次の図は、これらのブログを設定する必要がないことを証明するための最良の証拠です。