1.确保安装了IDEA和TestNg插件了 。 如果找不到run或者无法显示包,可能是源代码的默认路径设置问题,或者一个文件有多个public(内部)类,导致的无法识别。
1)windows 下 Chromedriver.exe 下载并放到python的Scripts路径下 与selenium版本需要对应
2)使用pip安装selenuim:selenium pip install selenium (需求安装了python和pip)
3)查看是否已经安装: pip show selenium
4)测试一下是否安装成功了
编写代码:
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from selenium import webdriver # 导入webdriver包
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
5)遇到使用selenium时提示:ImportError:No module named selenium
命令行执行 python -m pip install -U selenium (主要是因为安装了python的虚拟环境,导致selenuim被安装的路径不是和Pycharm的同一路径)
2.新建maven项目,pom文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qutoutiao</groupId>
<artifactId>selenuim</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>[2.53.0,)</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>[2.53.0,)</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgs>
<arg>-Xlint:unchecked</arg>
<arg>-Xlint:deprecation</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<suiteXmlFiles>
<file>res/testNG.xml</file>
</suiteXmlFiles>
<!--<workingDirectory>target/</workingDirectory>-->
</configuration>
</plugin>
</plugins>
</build>
</project>
3.编写单元测试类
package com.qutoutiao;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;
public class TestSelenuim {
private static WebDriver driver;
@BeforeMethod
public void beforeMethod() {
driver = new ChromeDriver();
System.out.println("1.创建浏览器driver对象");
}
@AfterMethod
public void afterMethod() {
System.out.println("7.关闭窗口");
driver.quit();//关闭浏览器,close关闭窗口
}
@Test
public void testSelenuim() throws IOException, InterruptedException {
driver.manage().window().maximize();
System.out.println("2.窗口最大化");
driver.get("http://www.baidu.com");
// Point p = new Point(500, 300);//指定窗口在屏幕中出现位置
// driver.manage().window().setPosition(p);
System.out.println("3.发送get请求");
//设置等待页面加载完毕
int pageLoadTime = 10;
driver.manage().timeouts().pageLoadTimeout(pageLoadTime, TimeUnit.SECONDS);
WebElement element = driver.findElement(By.name("wd"));
element.sendKeys("hello Selenium!");
System.out.println("4.输入查询关键字");
//提交表单
//element.submit();
driver.findElement(By.xpath("//*[@id='su']")).click();
System.out.println("5.表达提交");
//截屏操作图片已当前时间命名
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); //转换时间格式
String time = dateFormat.format(Calendar.getInstance().getTime()); //获取当前时间
//等待页面再截图
Thread.sleep(3000);
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); //执行屏幕截取
//利用FileUtils工具类的copyFile()方法保存getScreenshotAs()返回的文件;"屏幕截图"即时保存截图的文件夹
FileUtils.copyFile(srcFile, new File("屏幕截图", time + ".png"));
System.out.println("6.截图名称:" + time + ".png");
Thread.sleep(1000);
}
}
控制台日志:
[TestNG] Running:
C:\Users\admin\.IntelliJIdea2018.2\system\temp-testng-customsuite.xml
Starting ChromeDriver 2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e) on port 18040
Only local connections are allowed.
十一月 27, 2018 7:44:57 下午 org.openqa.selenium.remote.ProtocolHandshake createSession
信息: Detected dialect: OSS
1.创建浏览器driver对象
2.窗口最大化
3.发送get请求
4.输入查询关键字
5.表达提交
6.截图名称:20181127194459.png
7.关闭窗口
===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
Process finished with exit code 0
浏览器:
python中文文档https://selenium-python-zh.readthedocs.io/en/latest/navigating.html