-1
硒的webdriver的Java如何在硒中執行js?
任何人知道如何執行這個腳本js.executeScript("window.addEventListener('load',function(){document.getElementsByTagName('script')[1].src='google.com'})");
或
js.executeScript("window.load = function()
{document.getElementsByTagName('script')[1].src='google.com'})");
這個腳本返回null。 Eclipse中說:異常線程 「main」 顯示java.lang.NullPointerException 在autotesting.itacademy.main(itacademy.java:58)
這裏主要的代碼是:提前
package autotesting;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
//for read and write from file
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;
public class itacademy {
private static WebDriver driver = null;
public static JavascriptExecutor js = (JavascriptExecutor) driver;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
JavascriptExecutor js = (JavascriptExecutor) driver;
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("http://office.co.uk/");
js.executeScript("window.addEventListener('load',function(){document.getElementsByTagName('script')[1].src='google.com'})");
}}
謝謝
請分享您的堆棧跟蹤... – Tgsmith61591
@ Tgsmith61591異常線程 「main」 顯示java.lang.NullPointerException在autotesting.itacademy.main(itacademy.java:58) – Jack
@ Tgsmith61591而且這個例外在線程「主「java.lang.IllegalStateException:顯示java.lang.NullPointerException \t在autotesting.itacademy.main(itacademy.java:58) – Jack