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

查看:25
本文介绍了如何使用 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 文件放在我的本地主机上,我使用 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 的检查元素检查 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

您可能正在使用浏览器机器人来获取当前的句柄测试的窗口或文档.幸运的是,WebDriver 总是在当前窗口的上下文中评估 JS,因此您可以使用直接窗口"或文档".

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.

或者,您可能正在使用浏览器机器人来定位元素.在 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天全站免登陆