简体   繁体   English

Selenium WebDriver首先设置了Java示例

[英]Selenium WebDriver first Java example to set up

I created pom.xml and an example file Selenium2Example.java to set up Maven. 我创建了pom.xml和一个示例文件Selenium2Example.java来设置Maven。 I followed the instructions on here http://seleniumhq.org/docs/03_webdriver.html#chrome-driver 我按照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 在运行它时出现以下错误运行为 - >运行配置 - >(在TestNG下选择Selenium2Example.java

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)

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 尝试右键单击要运行的文件 - >运行为 - > Java应用程序

That should fix your Problem... 那应该解决你的问题......

If you want to do some testing with Selenium WebDriver, don't forget to choose a Testing- Framework. 如果您想使用Selenium WebDriver进行一些测试,请不要忘记选择Testing-Framework。 I would recommend JUnit or even better TestNG... 我会推荐JUnit甚至更好的TestNG ......

There is no issue with the java code. java代码没有问题。 Pl check the installation of the correct driver.. Try by changing the chrome driver with the firefox driver. Pl检查安装正确的驱动程序。尝试使用firefox驱动程序更改chrome驱动程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM