突然收到有关phantomjs的错误

时间:2019-01-26 00:05:46

标签: java selenium-webdriver phantomjs

更新:删除外部导入的jar文件并仅选择正确的jar文件后,出现了新错误


Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/openqa/selenium/browserlaunchers/Proxies
    at org.openqa.selenium.phantomjs.PhantomJSDriverService.createDefaultService(PhantomJSDriverService.java:178)
    at org.openqa.selenium.phantomjs.PhantomJSDriver.<init>(PhantomJSDriver.java:96)
    at org.openqa.selenium.phantomjs.PhantomJSDriver.<init>(PhantomJSDriver.java:86)
    at test.ttest.initialize(ttest.java:97)
    at test.ttest.<init>(ttest.java:85)
    at test.ttest$1.run(ttest.java:72)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.browserlaunchers.Proxies
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 20 more

这周我在做学校项目,突然出现了这个错误,

我尝试了很多事情,没有任何效果,我不知道为什么会这样,

我最后尝试的方法是仅导入我需要的外部jars

问题似乎出在此行WebDriver driver= new PhantomJSDriver();

这是旧错误

Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: org.openqa.selenium.os.CommandLine.find(Ljava/lang/String;)Ljava/lang/String;
    at org.openqa.selenium.phantomjs.PhantomJSDriverService.findPhantomJS(PhantomJSDriverService.java:233)
    at org.openqa.selenium.phantomjs.PhantomJSDriverService.createDefaultService(PhantomJSDriverService.java:182)
    at org.openqa.selenium.phantomjs.PhantomJSDriver.<init>(PhantomJSDriver.java:96)
    at org.openqa.selenium.phantomjs.PhantomJSDriver.<init>(PhantomJSDriver.java:86)
    at Login.initialize(Login.java:182)
    at Login.<init>(Login.java:96)
    at Login$1.run(Login.java:79)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

这是完整的代码

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
//import javax.swing.border.EmptyBorder;

import javax.imageio.ImageIO;
/*
import java.io.IOException;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
*/
import org.openqa.selenium.phantomjs.PhantomJSDriver;

import gherkin.deps.net.iharder.Base64.InputStream;
import gherkin.deps.net.iharder.Base64.OutputStream;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
//import javax.swing.plaf.metal.MetalBorders.TextFieldBorder;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
//import java.awt.Point;
//import org.openqa.selenium.firefox.FirefoxDriver;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

import javax.swing.JPanel;


//import java.io.File;
//import java.io.IOException;
//import java.util.concurrent.TimeUnit;
//import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
//import org.openqa.selenium.TakesScreenshot;
//import org.openqa.selenium.chrome.ChromeDriver;



public class Login extends JFrame {
    private JFrame frame;
    private JTextField CIN;
    private JTextField daten;
    private JLabel background;
    private JLabel cinlabel;
    private JLabel lblNewLabel_1;
    private JLabel datalabel;
    private JTextField code;
    private JLabel codelabel;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Login window = new Login();
                    //window.frame.setUndecorated(true);
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
        //PropertyConfigurator.configure("C:\\Users\\MJ\\eclipse-workspace\\FindMyResults\\src\\log4j.properties");
    }

    /**
     * Create the application.
     * @throws IOException 
     */
    public Login() throws IOException {
        initialize();

    }

    /**
     * Initialize the contents of the frame.
     * @throws IOException 
     */
    private void initialize() throws IOException {


        frame = new JFrame();
        frame.setBounds(100, 100, 1024, 768);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        codelabel = new JLabel("Code de s\u00E9curit\u00E9 \t");
        codelabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
        codelabel.setBounds(764, 374, 182, 14);
        frame.getContentPane().add(codelabel);

        cinlabel = new JLabel("CIN ou Identifiant DGCI ");
        cinlabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
        cinlabel.setBounds(574, 169, 294, 14);
        frame.getContentPane().add(cinlabel);

        datalabel = new JLabel("Date de naissance \t");
        datalabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
        datalabel.setBounds(574, 278, 182, 14);
        frame.getContentPane().add(datalabel);

        lblNewLabel_1 = new JLabel("");
        lblNewLabel_1.setIcon(new ImageIcon("C:\\Users\\MJ\\Desktop\\giphy.gif"));
        lblNewLabel_1.setBounds(616, 64, 440, 90);
        frame.getContentPane().add(lblNewLabel_1);

        CIN = new JTextField();
        CIN.setBackground(new Color(248, 248, 255));
        CIN.setSelectedTextColor(new Color(165, 42, 42));
        CIN.setSelectionColor(new Color(175, 238, 238));
        CIN.setBounds(574, 201, 396, 50);
        CIN.setToolTipText("");
        CIN.setFont(new Font("Helvetica", Font.PLAIN, 18));
        frame.getContentPane().add(CIN);
        CIN.setColumns(10);

        daten = new JTextField();
        daten.setBackground(new Color(248, 248, 255));
        daten.setBounds(574, 303, 396, 50);
        daten.setForeground(new Color(0, 0, 0));
        daten.setFont(new Font("Helvetica", Font.PLAIN, 18));
        daten.setColumns(10);
        frame.getContentPane().add(daten);


/* bypass ssl certificate check
        // Create a trust manager that does not validate certificate chains
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            }

            public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            }
        } };

        // Install the all-trusting trust manager
        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (Exception e) {
        }

        try {
            editorPane.setPage(url);
            //editorPane.scrollToReference(reference);
        } catch (IOException e) {
            System.err.println("Attempted to read a bad URL: " + url);
        }

        */

        //System.setProperty("phantomjs.binary.path","./phantomjs.exe");
        System.setProperty("phantomjs.binary.path", "phantomjs.exe");
        //System.setProperty("phantomjs.binary.path", "C:\\Users\\MJ\\Desktop\\phantomjs.exe");

        WebDriver driver = new PhantomJSDriver();

        driver.get("https://www4.inscription.tn/ORegMx/servlet/AuthentificationEtud?ident=cin");
        String title1=driver.getTitle();
        if (!(title1.equals("Site de l'inscription universitaire en ligne"))) {
            JOptionPane.showMessageDialog(null,"Verifiez votre connexion internet");
            driver.quit();

        }
        WebElement ele = driver.findElement(By.xpath("//td[@rowspan='2']"));

        driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        // Get entire page screenshot
        File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        BufferedImage  fullImg = ImageIO.read(screenshot);

        // Get the location of element on the page
        org.openqa.selenium.Point point = ele.getLocation();

        // Get width and height of the element
        int eleWidth = ele.getSize().getWidth();
        int eleHeight = ele.getSize().getHeight();

        // Crop the entire page screenshot to get only element screenshot
        BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
            eleWidth, eleHeight);
        ImageIO.write(eleScreenshot, "png", screenshot);

        // Copy the element screenshot to disk
        File screenshotLocation = new File("temp\\captcha.png");
        FileUtils.copyFile(screenshot, screenshotLocation); 


        JLabel lblNewLabel = new JLabel("");
        lblNewLabel.setIcon(new ImageIcon("temp\\captcha.png"));
        lblNewLabel.setBounds(574, 399, 182, 50);
        frame.getContentPane().add(lblNewLabel);


        JButton valider = new JButton("Valider");
        valider.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {




                WebElement c = driver.findElement(By.name("cin"));
                WebElement d = driver.findElement(By.id("dn"));
                WebElement cap = driver.findElement(By.name("cincap"));
                String cin = CIN.getText();
                String date = daten.getText();
                String capp = code.getText();

                // fill the fields
                c.sendKeys(cin);
                d.sendKeys(date);
                cap.sendKeys(capp);

                // button valider
                cap.submit();

                // check the title of the page
                String title=driver.getTitle();
                if (title.equals("Dashboard - Inscription universitaire en ligne")) {


                System.out.println("Bienvenue");   //modify to new frame soon
                WebElement carteEtd = driver.findElement(By.xpath("//p[contains(text(),'Carte Etudiant')]"));
                Actions builder = new Actions(driver);
                builder.moveToElement(carteEtd).click(carteEtd);
                builder.perform();
                System.out.println("test"+carteEtd);
                JOptionPane.showMessageDialog(null,carteEtd);
                    //frame.dispose();
                    //Home bien=new Home();
                    //bien.setVisible(true);
                // driver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS);
                }
                else {
                    WebElement msg = driver.findElement(By.xpath("/html[1]/body[1]/table[1]/tbody[1]/tr[1]/td[1]/table[1]/tbody[1]/tr[1]/td[1]/div[1]/blockquote[1]/div[1]/font[1]"));
                    String error=msg.getAttribute("innerText");
                    //System.out.println(error);
                    JOptionPane.showMessageDialog(null,error);



                    WebElement ele = driver.findElement(By.xpath("//td[@rowspan='2']"));
                    driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
                    // Get entire page screenshot
                    File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
                    BufferedImage fullImg = null;
                    try {
                        fullImg = ImageIO.read(screenshot);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    // Get the location of element on the page
                    org.openqa.selenium.Point point = ele.getLocation();

                    // Get width and height of the element
                    int eleWidth = ele.getSize().getWidth();
                    int eleHeight = ele.getSize().getHeight();

                    // Crop the entire page screenshot to get only element screenshot
                    BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
                        eleWidth, eleHeight);
                    try {
                        ImageIO.write(eleScreenshot, "png", screenshot);
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }

                    // Copy the element screenshot to disk
                    File screenshotLocation = new File("temp\\captcha.png");
                    try {
                        FileUtils.copyFile(screenshot, screenshotLocation);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }       



                    ImageIcon img=new ImageIcon("temp\\\\captcha.png");
                    img.getImage().flush();
                    lblNewLabel.setIcon(img);


                }
            }
        });
        valider.setFont(new Font("Tahoma", Font.BOLD, 13));
        valider.setBounds(722, 557, 104, 32);
        frame.getContentPane().add(valider);

        code = new JTextField();
        code.setBackground(new Color(248, 248, 255));
        code.setForeground(Color.BLACK);
        code.setFont(new Font("Helvetica", Font.PLAIN, 18));
        code.setColumns(10);
        code.setBounds(764, 399, 206, 50);
        frame.getContentPane().add(code);

        JPanel panel = new JPanel();
        panel.setBackground( new Color(0, 0, 0, 90) );
        panel.setBounds(0, 0, 511, 735);
        frame.getContentPane().add(panel);




        background = new JLabel("");
        background.setIcon(new ImageIcon("E:\\AnimationV.gif"));
        background.setBounds(0, 0, 1006, 724);
        frame.getContentPane().add(background);



    }
}

我为phantomjs尝试了很多东西,效果很好:/

我相信有一个简单的解决方案

我在Eclipse中使用windowbuilder, 谢谢

0 个答案:

没有答案