selenium调用js文件_如何使用selenium将javascript文件加载到DOM中?

news/2025/2/26 13:24:51

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

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

这是我的代码:

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 += '';");

}

}

我链接的javascript文件的代码是

alert("hi Nate");

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

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


http://www.niftyadmin.cn/n/712832.html

相关文章

如何模拟超过 5 万的并发用户

本文将从负载测试的角度,描述了做一次流畅的5万用户并发测试需要做的事情. 你可以在本文的结尾部分看到讨论的记录. 快速的步骤概要 1.编写你的脚本 2.使用JMeter进行本地测试 3.BlazeMeter沙箱测试 4.使用一个控制台和一个引擎设置Users-per-Engine的数量 5.设置并…

CodeForces 629A-Far Relative’s Birthday Cake(枚举/暴力)

题目描述: Doors family is going celebrate Famil Doorss birthday party. They love Famil Door so they are planning to make his birthday cake weird! The cake is a nn nn nn square consisting of equal squares with side length 1 1 1 . Each square is e…

bzoj [Usaco2009 Hol]Cattle Bruisers 杀手游戏

Description Input 第1行输入N,R,BX,BY, BVX,BVY,之后N行每行输入四个整数Xi,Yi,VXi,VYi. Output 一个整数,表示在逃脱过程中,某一个时刻最多有这…

乐玩自动化测试模块_Python自动化测试——必会模块 Unittest

前言:一直在努力做测试的小白白 个人觉得使用python标准库中的Unittest搭建自动化测试框架很好用所以在这里做个笔记。Unittest模块核心概念非为四层先后顺序可以为TestFixture->TestCase->TestSuite->TestRunner Surprise MotherF*cker 跟这个图有什么关系…

DES加密解密

自己写的DES加密解密类,加密后生成Base64字符串,并去除字符。 加密后替换掉,这样加密后的字符串可以作为url参数传递。 using System; using System.IO; using System.Security.Cryptography; using System.Text;namespace QuaEdu.Helper {//…

bootstrap下拉选择框选中事件_基于jQuery的select下拉框选择触发事件实例分析

本文实例讲述了基于jQuery的select下拉框选择触发事件实现方法。分享给大家供大家参考,具体如下:我一直以来都认为,select 下拉框选择对选项 options 使用 onclick 注册事件即可,如下:选项一选项二今天有个要求需要做联…

麒麟开源堡垒主机在等保上的合规性分析

信息安全等级保护工作包括定级、备案、安全建设和整改、信息安全等级测评、信息安全检查五个阶段。 我国的信息安全等级保护共分为五级,级别越高,要求越严格。 我国的信息安全等级保护主要标准包括,《信息系统等级保护安全设计技术要求&…

CodeForces 6A-Triangle(枚举/暴力)

题目描述: Johnny has a younger sister Anne, who is very clever and smart. As she came home from the kindergarten, she told his brother about the task that her kindergartener asked her to solve. The task was just to construct a triangle out of fo…