使用 selenium 实现 jquery 时出错

Getting error when implement jquery with selenium

以下代码尝试从 Facebook 页面中删除一个项目并将其附加到另一个 ID。但是我得到一个错误,我找不到问题所在。

WebDriver driver = 新 FirefoxDriver();

@BeforeTest
public void launchbrowser(){
    String baseUrl = "http://www.facebook.com";
    driver.get(baseUrl);
    driver.findElement(By.id("email")).sendKeys("anyman@hotmail.com");
    driver.findElement(By.id("pass")).sendKeys("deltaduck");
    driver.findElement(By.id("loginbutton")).click();

}

@Test
public void test() throws Exception {


    remove();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    append();

}


protected void click(String elementId){
    String script ="document.getElementById('" + elementId + "').click();";

    executeJavascript(script);


}


protected void remove(){
    String remove ="$('userNavigationLabel').remove();";
    executeJavascript(remove);
}

protected void append(){
    String append ="$('privacyFlyoutLabel').append('u_0_f');";
    executeJavascript(append);
}

private void executeJavascript(String script){

    JavascriptExecutor je = (JavascriptExecutor) driver;
    try {
        Thread.sleep(1500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    je.executeScript(script);       
}

}
我收到的错误消息是:

`.org.openqa.selenium.WebDriverException: $(...).append 不是函数 命令持续时间或超时:26 毫秒

org.openqa.selenium.WebDriverException: $(...).append 不是函数 命令持续时间或超时:26 毫秒 构建信息:版本:'2.43.0',修订:'accb3003b9fb8f7cae30f9669b4c594a065396a6',时间:'2014-09-09 22:22:51' 系统信息:host: 'JARVIS', ip: '140.203.209.182', os.name: 'Windows 8', os.arch: 'x86', os.version: ' 6.2', java.version: '1.7.0_45' Session 编号:a8a7480e-8e66-499b-b660-803e10e91197 Driver 信息:org.openqa.selenium.firefox.FirefoxDriver 能力 [{platform=WINDOWS, databaseEnabled=true, cssSelectorsEnabled=true, javascriptEnabled=true, acceptSslCerts=true, handlesAlerts=true, browserName=firefox, webStorageEnabled=true, nativeEvents=true, rotatable=false, locationContextEnabled=true , applicationCacheEnabled=true, takesScreenshot=true, version=31.0}] 在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(未知来源) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(来源不明) 在 java.lang.reflect.Constructor.newInstance(来源不明) 在 org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204) 在 org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156) 在 org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599) 在 org.openqa.selenium.remote.RemoteWebDriver.execute脚本(RemoteWebDriver.java:508) 在 Main.executeJavascript(Main.java:65) 在 Main.append(Main.java:54) 在 Main.test(Main.java:33) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 在 sun.reflect.NativeMethodAccessorImpl.invoke(未知来源) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(来源不明) 在 java.lang.reflect.Method.invoke(来源不明) 在 org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84) 在 org.testng.internal.Invoker.invokeMethod(Invoker.java:714) 在 org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) 在 org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) 在 org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) 在 org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) 在 org.testng.TestRunner.privateRun(TestRunner.java:767) 在 org.testng.TestRunner.run(TestRunner.java:617) 在 org.testng.SuiteRunner.runTest(SuiteRunner.java:334) 在 org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) 在 org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) 在 org.testng.SuiteRunner.run(SuiteRunner.java:240) 在 org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 在 org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 在 org.testng.TestNG.runSuitesSequentially(TestNG.java:1224) 在 org.testng.TestNG.runSuitesLocally(TestNG.java:1149) 在 org.testng.TestNG.run(TestNG.java:1057) 在 org.testng.remote.RemoteTestNG.run(远程TestNG.java:111) 在 org.testng.remote.RemoteTestNG.initAndRun(远程TestNG.java:204) 在 org.testng.remote.RemoteTestNG.main(远程TestNG.java:175) 由以下原因引起:org.openqa.selenium.WebDriverException:$(...).append 不是

您 运行 遇到的问题是在 Facebook 页面上,$ 不是 jQuery。我不知道它是什么,但调用 $('privacyFlyoutLabel') 将 return 具有 ID privacyFlyoutLabel 的元素。使用 jQuery 这将 return 一个元素,其 标签名称 privacyFlyoutLabel。要使用 id privacyFlyoutLabel 和 jQuery 获取元素,您必须请求 $('#privacyFlyoutLabel').

您从 $ 得到的是 DOM 节点,而不是 jQuery 对象,因此没有 .append 方法。

选项:

  1. 忘记 jQuery。编写直接访问 DOM 的代码。

  2. 尝试在该页面中加载 jQuery,然后再执行您要执行的操作。这需要添加一个 script 元素来加载 jQuery 并调用 jQuery.noConflict() 来恢复 $。但问题是,无法保证 jQuery.noConflict 会在 之前恢复 $ 页面上已经 运行 的任何异步代码需要使用它。我对 setTimeout 进行了一些测试,并确定之前启动的异步操作可以在两个 script 元素之间执行。所以这个选项实际上可能导致 Facebook 依赖的代码失败。