关闭

【selenium】selenium安装及验证

1971人阅读 评论(0) 收藏 举报

安装前提

  • jdk安装好
  • eclipse安装好
  • maven安装好

selenium安装

  • 在项目路径上新建文件夹seleniumtestP(E:\seleniumtestP),无中文
  • 新建文件Pom.xml
    <?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>MySel20Proj</groupId>
            <artifactId>MySel20Proj</artifactId>
            <version>1.0</version>
            <dependencies>
                <dependency>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-java</artifactId>
                    <version>2.19.0</version>
                </dependency>
            </dependencies>
    </project>
    
  • 进入到命令行界面,进入到第三步建立的Pom.xml文件所在的路径(cd E:\seleniumtestP)
  • 执行命令mvn clean install
  • 执行成功后:项目下载完成,为了导入eclipse,执行转化命令 :mvn eclipse:eclipse
  • 将转化后的项目导入到eclipse中,file>import>existing projects into workspace,选择转化后的项目,点击确定导入。
  • 导入后项目会报错,以为引用的jar没有被加入索引路径。Window>preferences>Java>Build Path>Classpath Variables,点击new新建 name: M2_REPO path:maven的jar本地路径即(.m2/repository)
  • 确定后项目不再报错
  • 项目中建立源码文件夹:src(项目上右键,new,Source Folder)
  • 新建包,com.test和包下的java文件
  • 完成Java文件内容,并再联网的情况下运行。

public class Selenium2Example  {
    public static void main(String[] args) {
        // Create a new instance of the Firefox driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        WebDriver driver = new FirefoxDriver();
        // And now use this to visit Google
        driver.get("http://www.google.com");
        // Alternatively the same thing can be done like this
        // driver.navigate().to("​​http://www.google.com.hk");
        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("q"));
        // Enter something to search for
        element.sendKeys("Cheese!");
        // Now submit the form. WebDriver will find the form for us from the element
        element.submit();
        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());
        // Google's search is rendered dynamically with JavaScript.
        // Wait for the page to load, timeout after 10 seconds
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith("cheese!");
            }
        });

        // Should see: "cheese! - Google Search"
        System.out.println("Page title is: " + driver.getTitle());
        
        //Close the browser
        driver.quit();
    }
}

  • 正常运行即可

问题

  • 报错:Exception in thread "main"org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed.
  • 解决办法:指定firefox执行文件入系统属性。
       
     System.setProperty("webdriver.firefox.bin","D:/Program Files/Mozilla Firefox/firefox.exe");
           WebDriver driver =new FirefoxDriver();
    

学习地址:

  • 另一学习:

selenium-ide安装及验证

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:213098次
    • 积分:3102
    • 等级:
    • 排名:第11493名
    • 原创:90篇
    • 转载:5篇
    • 译文:0篇
    • 评论:87条