当前位置: 首页 > 面试题库 >

如何使用Java使用Selenium WebDriver实现PhantomJS

庾君博
2023-03-14
问题内容

我真的要疯了。我有以下代码:

public class Creazione extends TestCase {
    private PhantomJSDriver driver;
    private String baseUrl;
    private boolean acceptNextAlert = true;
    private StringBuffer verificationErrors = new StringBuffer();

    @Before
    public void setUp() throws Exception {
        File file = new File("C:/Program Files/phantomjs-1.9.7-windows/phantomjs.exe");
        System.setProperty("phantomjs.binary.path", file.getAbsolutePath());
        driver = new PhantomJSDriver();
        baseUrl = "http://www.gts.fiorentina.test/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get(baseUrl + "/Account/Login.aspx?ReturnUrl=%2f");
        findDynamicElement(By.id("tbUserName_I"), 2000);
        driver.findElement(By.id("tbUserName_I")).clear();
        driver.findElement(By.id("tbUserName_I")).sendKeys("rogai");
        driver.findElement(By.id("tbPassword_I")).clear();
        driver.findElement(By.id("tbPassword_I")).sendKeys("Fiorentina2014!");
        driver.findElement(By.id("btnLogin_CD")).click();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void testCreazione() throws Exception {
        driver.get(baseUrl + "/SegreteriaSportiva/Calciatori.aspx");
        findDynamicElement(By.cssSelector("#ASPxButton1_CD > span"), 2000);
        driver.findElement(By.cssSelector("#ASPxButton1_CD > span")).click();
        findDynamicElement(By.id("FrmEdit_TS_TipoPersonaId_FK_B-1"), 2000);
        driver.findElement(By.id("FrmEdit_TS_TipoPersonaId_FK_B-1")).click();
        driver.findElement(By.id("FrmEdit_TS_TipoPersonaId_FK_DDD_L_LBI2T0")).click();
        driver.findElement(By.id("FrmEdit_Nome_I")).clear();
        driver.findElement(By.id("FrmEdit_Nome_I")).sendKeys("Prova");
        driver.findElement(By.id("FrmEdit_Cognome_I")).clear();
        driver.findElement(By.id("FrmEdit_Cognome_I")).sendKeys("Calciatore");
        driver.findElement(By.id("FrmEdit_TS_RuoloId_FK_B-1")).click();
        driver.findElement(By.id("FrmEdit_TS_RuoloId_FK_DDD_L_LBI3T0")).click();
        driver.findElement(By.id("FrmEdit_DataNascita_I")).clear();
        driver.findElement(By.id("FrmEdit_DataNascita_I")).sendKeys("01/01/2014");
        driver.findElement(By.id("FrmEdit_Cittadinanza_I")).clear();
        driver.findElement(By.id("FrmEdit_Cittadinanza_I")).sendKeys("italiana");
        driver.findElement(By.id("FrmEdit_LuogoNascita_I")).clear();
        driver.findElement(By.id("FrmEdit_LuogoNascita_I")).sendKeys("roma");
        driver.findElement(By.cssSelector("#BTN_Edit_CD > span")).click();
        driver.findElement(By.id("Grid_DXFREditorcol3_I")).click();
        driver.findElement(By.id("Grid_DXFREditorcol3_I")).sendKeys("Prova");
        assertEquals("Prova",driver.findElement(By.xpath("//tr[@id='Grid_DXDataRow0']/td[3]")).getText());
    }
}

执行后,我在运行时遇到这些错误,以下是该跟踪-

org.openqa.selenium.InvalidElementStateException: {"errorMessage":"Element is not currently interactable and may not be manipulated","request":{"headers":{"Accept-Encoding":"gzip,deflate","Connection":"Keep-Alive","Content-Length":"27","Content-Type":"application/json; charset=utf-8","Host":"localhost:20497","User-Agent":"Apache-HttpClient/4.3.2 (java 1.5)"},"httpVersion":"1.1","method":"POST","post":"{\"id\":\":wdc:1402393987914\"}","url":"/clear","urlParsed":{"anchor":"","query":"","file":"clear","directory":"/","path":"/clear","relative":"/clear","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/clear","queryKey":{},"chunks":["clear"]},"urlOriginal":"/session/e4b71580-f084-11e3-959e-273aa801dd81/element/%3Awdc%3A1402393987914/clear"}}
Command duration or timeout: 215 milliseconds
Build info: version: '2.42.1', revision: '68b415a', time: '2014-05-29 16:17:18'
System info: host: 'Silvio-Acer', ip: '10.10.1.122', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_55'
Session ID: e4b71580-f084-11e3-959e-273aa801dd81
Driver info: org.openqa.selenium.phantomjs.PhantomJSDriver
Capabilities [{platform=XP, acceptSslCerts=false, javascriptEnabled=true, browserName=phantomjs, rotatable=false, driverVersion=1.1.0, locationContextEnabled=false, version=1.9.7, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=false, browserConnectionEnabled=false, webStorageEnabled=false, nativeEvents=true, proxy={proxyType=direct}, applicationCacheEnabled=false, driverName=ghostdriver, takesScreenshot=true}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:600)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
    at org.openqa.selenium.remote.RemoteWebElement.clear(RemoteWebElement.java:113)
    at com.example.tests.Creazione.testCreazione(Creazione.java:59)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at junit.framework.TestCase.runTest(TestCase.java:176)
    at junit.framework.TestCase.runBare(TestCase.java:141)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:255)
    at junit.framework.TestSuite.run(TestSuite.java:250)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.openqa.selenium.remote.ScreenshotException: Screen shot has been taken
Build info: version: '2.42.1', revision: '68b415a', time: '2014-05-29 16:17:18'
System info: host: 'Silvio-Acer', ip: '10.10.1.122', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_55'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:136)
    ... 23 more
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: {"errorMessage":"Element is not currently interactable and may not be manipulated","request":{"headers":{"Accept-Encoding":"gzip,deflate","Connection":"Keep-Alive","Content-Length":"27","Content-Type":"application/json; charset=utf-8","Host":"localhost:20497","User-Agent":"Apache-HttpClient/4.3.2 (java 1.5)"},"httpVersion":"1n.1","method":"POST","post":"{\"id\":\":wdc:1402393987914\"}","url":"/clear","urlParsed":{"anchor":"","query":"","file":"clear","directory":"/","path":"/clear","relative":"/clear","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/clear","queryKey":{},"chunks":["clear"]},"urlOriginal":"/session/e4b71580-f084-11e3-959e-273aa801dd81/element/%3Awdc%3A1402393987914/clear"}}
Build info: version: '2.42.1', revision: '68b415a', time: '2014-05-29 16:17:18'
System info: host: 'Silvio-Acer', ip: '10.10.1.122', os.name: 'Windows 7', os.arch: 'x86',       os.version: '6.1', java.version: '1.7.0_55'
    Driver info: driver.version: unknow

我已经将phantomjs.exe添加到Windows路径中,将ghostdriver.jar添加为外部库,但是什么也没有…

我想让它工作两天了…


问题答案:

试试这个,对我有用

DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);                
caps.setCapability("takesScreenshot", true);  
caps.setCapability(
                        PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
                        "your custom path\\phantomjs.exe"
                    );
WebDriver driver = new  PhantomJSDriver(caps);

然后其余的类似。.并且请评论您的观察,祝您有美好的一天:)



 类似资料:
  • 问题内容: 我如何在seleniumwebdriver 3.0 beta版本中使用geckodriver。当我实例化Firefox时: 我得到错误: 线程“主”中的异常java.lang.IllegalStateException:驱动程序可执行文件的路径必须由webdriver.gecko.driver系统属性设置;否则,必须执行以下操作:有关更多信息,请参见 https://github.co

  • 我使用PHP处理以下输入:sam 99912222 tom 1112222 harry 12299933 sam edward harry第1到第6行是姓名和电话号码。最后三行是搜索查询,如果姓名不在列表中(没有电话号码,打印未找到),否则输出数据。我的代码如下: 例外输出应为sam=99912222未找到harry=12299933输出为sam=99912222未找到未找到。为什么这些功能不起作

  • 我正在尝试播放以下网站的视频(使用JUnit)-Day01。http://www.itelearn.com/live-training/security-testing-live-training我试图实现的是,在播放视频后,我将拍摄一张屏幕截图,以证明视频播放正确。点击Day01视频后,它会在一个新窗口中打开——当我查看代码时,我意识到他们使用了iFrame。我可以关闭此视频窗口,但无法播放/暂

  • 问题内容: 我很快将使用Java的log4j类来创建项目。但是我不认为我对此有任何了解。希望有人能启发我这个小问题。 问题答案: Log4j基本上接受您要输出的任何语句,让您为其分配“严重性”级别(警告,错误,严重等),并以多种方式将其注销。(对于文件,字节流等,有很多附加选项可用。) 这是对log4j的简短介绍。 http://www.developer.com/open/article.php

  • 问题陈述:我试图在网页上获取span标记的值。*该网页上有多个span标记,其中我需要4个。

  • 本文向大家介绍在Java中如何使用lambda实现LongUnaryOperator?,包括了在Java中如何使用lambda实现LongUnaryOperator?的使用技巧和注意事项,需要的朋友参考一下 LongUnaryOperator是一个java.util.function函数接口。此函数接口接受单个长值操作数并生成长值结果。LongUnaryOperator接口可以用作lambda表达