2015-11-12 70 views
-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'})"); 
      }} 

謝謝

+0

請分享您的堆棧跟蹤... – Tgsmith61591

+0

@ Tgsmith61591異常線程 「main」 顯示java.lang.NullPointerException在autotesting.itacademy.main(itacademy.java:58) – Jack

+0

@ Tgsmith61591而且這個例外在線程「主「java.lang.IllegalStateException:顯示java.lang.NullPointerException \t在autotesting.itacademy.main(itacademy.java:58) – Jack

回答

3

你永遠不會初始化你的JavascriptExecutor。看看這裏,您分配driver爲空,然後嘗試將它轉換爲JavascriptExecutor,從不初始化:

private static WebDriver driver = null; // driver is null 
public static JavascriptExecutor js = (JavascriptExecutor) driver; // js is null 

後來,分配jsdriver然後初始化驅動程序對象,在這裏:

JavascriptExecutor js = (JavascriptExecutor) driver; // js is null 
driver = new FirefoxDriver(); // driver is now an object 

而是看你做什麼用js的時候,它仍然是空:

js.executeScript("window.addEventListener('load',function({document.getElementsByTagName('script')[1].src='google.com'})"); // It's still null here! 

您需要正確初始化您的js對象。順便說一句,您已經宣佈js爲班級成員,爲什麼您立即將其重新分配給主要成員?

public static JavascriptExecutor js = (JavascriptExecutor) driver; 
JavascriptExecutor js = (JavascriptExecutor) driver; // This line is unnecessary in main