如何使用selenium将javascript文件加载到DOM中? [英] How do I load a javascript file into the DOM using selenium?

查看:115
本文介绍了如何使用selenium将javascript文件加载到DOM中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium WebDriver尝试将外部javascript文件插入到DOM中,而不是将整个内容输入到executeScript中。

I'm using Selenium WebDriver to try to insert an external javascript file into the DOM, rather than type out the entire thing into executeScript.

看起来像它正确地将节点放入DOM中,但它只是忽略了源,即所述源js文件上的函数没有运行。

It looks like it properly places the node into the DOM, but then it just disregards the source, i.e. the function on said source js file doesn't run.

这是我的代码:

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Example  {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://google.com");
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("document.getElementsByTagName('head')[0].innerHTML += '<script src=\"<PATH_TO_FILE>\" type=\"text/javascript\"></script>';");
    }
}

我链接到的javascript文件的代码是

The code of the javascript file I am linking to is

alert("hi Nate");

我已将js文件放在我的localhost上,我使用file:///调用它,我在外部服务器上试了一下。没有骰子。

I've placed the js file on my localhost, I called it using file:///, and I tried it on an external server. No dice.

此外,在Java部分,我尝试使用该技巧附加'scr'+'ipt',但它仍然无效。当我使用Firefox的inspect元素检查DOM时,我可以看到它正确加载脚本节点,所以我很困惑。

Also, in the Java portion, I tried appending 'scr'+'ipt' using that trick, but it still didn't work. When I inspect the DOM using Firefox's inspect element, I can see it loads the script node properly, so I'm quite confused.

我也试过这个解决方案,显然是为另一个版本的Selenium(而不是webdriver)而制作的,因此一点都不起作用:在selenium中加载包含有用测试函数的外部js文件

I also tried this solution, which apparently was made for another version of Selenium (not webdriver) and thus didn't work in the least bit: Load an external js file containing useful test functions in selenium

推荐答案

根据这个: http://docs.seleniumhq.org/docs/appendix_migrating_from_rc_to_webdriver.jsp


您可能正在使用browserbot来获取当前
窗口或测试文档的句柄。幸运的是,WebDriver总是
在当前窗口的上下文中评估JS,因此你可以直接使用
window或document。

You might be using the browserbot to obtain a handle to the current window or document of the test. Fortunately, WebDriver always evaluates JS in the context of the current window, so you can use "window" or "document" directly.

或者,您可能正在使用browserbot来定位元素。
在WebDriver中,执行此操作的习惯用法是首先找到元素
,然后将其作为参数传递给Javascript。因此:

Alternatively, you might be using the browserbot to locate elements. In WebDriver, the idiom for doing this is to first locate the element, and then pass that as an argument to the Javascript. Thus:

以下是在webdriver中工作吗?

So does the following work in webdriver?

WebDriver driver = new FirefoxDriver();
((JavascriptExecutor) driver)
  .executeScript("var s=window.document.createElement('script');\
  s.src='somescript.js';\
  window.document.head.appendChild(s);");

这篇关于如何使用selenium将javascript文件加载到DOM中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆