简体   繁体   English

如何在selenium webdriver java中实现jquery脚本

[英]How to implement jquery script in selenium webdriver java

I've created a script as the I want to use in another Selenium WebDriver script: 我已经创建了一个脚本,我想在另一个Selenium WebDriver脚本中使用:

$function() {
    $("pane1").hide(300);
});

I'm trying to figure out a way to call this script in my Selenium java code. 我正在试图找到一种方法来在我的Selenium java代码中调用这个脚本。

Calling jQuery functions from Selenium is done exactly the same way as calling any other. 从Selenium调用jQuery函数的方式与调用任何其他函数完全相同。 However, there are two issues with your code: 但是,您的代码有两个问题:

  1. You have $function , where you probably mean $(function . If you tried to execute the code in your question as-is you certainly got an error because of this. 你有$function ,你可能意味着$(function 。如果你试图在你的问题中执行代码,那么你肯定会因此而出错。

  2. Ok, let's say you fix that problem. 好吧,让我们说你解决了这个问题。 Now you have a $(function () {...}) call. 现在你有一个$(function () {...})调用。 This is not harmful but it is pointless as you are essentially saying "execute this function when the page has finished its initial load". 这不是有害的,但它没有意义,因为你基本上是说“当页面完成初始加载时执行此功能”。 If you use Selenium the way it is usually used, it won't return control to you until the page has finished its initial load, so there is no reason to wait for the page load. 如果您按照常用的方式使用Selenium,它将不会在页面完成初始加载之前将控制权返回给您,因此没有理由等待页面加载。

So: 所以:

((JavascriptExecutor) driver).executeScript("$('pane1').hide(300);");

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

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