Selenium WebDriver 设置的第一个 Java 示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12191754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:58:00  来源:igfitidea点击:

Selenium WebDriver first Java example to set up

javamavenselenium-webdriver

提问by R11G

I created pom.xml and an example file Selenium2Example.java to set up Maven. I followed the instructions on here http://seleniumhq.org/docs/03_webdriver.html#chrome-driver

我创建了 pom.xml 和一个示例文件 Selenium2Example.java 来设置 Maven。我按照这里的说明http://seleniumhq.org/docs/03_webdriver.html#chrome-driver

But I am getting the error "Selection does not contain a main type"on running Selenium2Example.java as Java Application.

但是我在将Selenium2Example.java 作为 Java 应用程序运行时收到错误“选择不包含主要类型”

Here is the pom.xml I am using

这是我正在使用的 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.25.0</version>
            </dependency>
            <dependency>
                <groupId>com.opera</groupId>
                <artifactId>operadriver</artifactId>
            </dependency>
        </dependencies>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>com.opera</groupId>
                    <artifactId>operadriver</artifactId>
                    <version>0.16</version>
                    <exclusions>
                        <exclusion>
                            <groupId>org.seleniumhq.selenium</groupId>
                            <artifactId>selenium-remote-driver</artifactId>
                        </exclusion>
                    </exclusions>
                </dependency>
            </dependencies>
        </dependencyManagement>
</project>

Here is the Selenium2Example.java I am using:

这是我正在使用的 Selenium2Example.java:

package org.openqa.selenium.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.Chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Selenium2Example  {
    public static void main(String[] args) {
        // Create a new instance of the Chrome driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        WebDriver driver = new ChromeDriver();
        // 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");
        // 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();
    }
}

On running it gives the following error Run as -> Run config -> (selecting Selenium2Example.java under TestNG

运行时会出现以下错误 Run as -> Run config -> (selecting Selenium2Example.java under TestNG

The error is

错误是

org.testng.TestNGException: 
Cannot find class in classpath: Selenium2Example
    at org.testng.xml.XmlClass.loadClass(XmlClass.java:76)
    at org.testng.xml.XmlClass.init(XmlClass.java:68)
    at org.testng.xml.XmlClass.<init>(XmlClass.java:54)
    at org.testng.xml.TestNGContentHandler.startElement(TestNGContentHandler.java:542)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:506)
    at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(AbstractXMLDocumentParser.java:182)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.emptyElement(XMLDTDValidator.java:766)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1302)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2715)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:488)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:302)
    at javax.xml.parsers.SAXParser.parse(SAXParser.java:195)
    at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:17)
    at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:10)
    at org.testng.xml.Parser.parse(Parser.java:172)
    at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:310)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:88)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

回答by Franz Ebner

First of all, you made the right decision (from my point) to use Maven, Java and WebDriver ;)

首先,您做出了正确的决定(从我的角度来看)使用 Maven、Java 和 WebDriver ;)

What you've done looks like you've selected the wrong package or file to run!

你所做的看起来你选择了错误的包或文件来运行!

That's not a problem of Selenium, it's a problem of your run configuration, your IDE!

这不是 Selenium 的问题,而是你的运行配置、你的 IDE 的问题!

I guess you use Eclipse?

我猜你用的是 Eclipse?

Try to right-click the file you want to run -> run as -> Java Application

尝试右键单击您要运行的文件 -> run as -> Java Application

That should fix your Problem...

那应该可以解决您的问题...

If you want to do some testing with Selenium WebDriver, don't forget to choose a Testing- Framework. I would recommend JUnit or even better TestNG...

如果你想用 Selenium WebDriver 做一些测试,不要忘记选择一个测试框架。我会推荐 JUnit 甚至更好的 TestNG ...

回答by Srikanth

There is no issue with the java code. Pl check the installation of the correct driver.. Try by changing the chrome driver with the firefox driver.

java代码没有问题。请检查是否安装了正确的驱动程序。尝试使用 Firefox 驱动程序更改 chrome 驱动程序。