0

我正在将 selenium webdriver 用于 JSP 上的应用程序。我添加的任何与硒有关的内容都会出错。它仅在我删除 WebElements 时才有效。有人知道我能做什么吗?

这是我的代码

 while(i < x)
 {

    i++;
    writer.println("\n\n"+"Number "+i +"\n\n");
    if(rs.next())
    {   

        WebElement acc = wait.until(ExpectedConditions.elementToBeClickable(By.id("x_account_"+i)));
        WebElement desc = driver.findElement(By.id("x_original_"+i));
        WebElement inf_desc = driver.findElement(By.id("x_infringement_"+i));
        WebElement twt_url = driver.findElement(By.id("x_url_"+i));
        List<WebElement> link_in_twt = driver.findElements(By.name("x_target_"+i));
        WebElement link_in_twt_1 = driver.findElement(By.id("x_website_direct_"+i));
        WebElement link_in_twt_conf = driver.findElement(By.id("x_website_direct_confirm_"+i));
        WebElement report_another_twt = driver.findElement(By.id("more_x_link"));

        String ACCOUNT = rs.getString("User");
        String DESC = "Sound and video recordings as performed by the " +
                "artist known as "+rs.getString("artist")+".";

        String INF_DESC = "This account is" +
                " incorrect.";

        String TWT_URL = rs.getString("FB_TW_Link");
        //-------------------------------------------------------------------
        try
        {
            acc.sendKeys(ACCOUNT);
            String Acc = "Account: "+ ACCOUNT +" - Complete";
            writer.println(Acc+"\n");
            //output.scrollRectToVisible(null);
            Thread.sleep(timer);
        }
        catch(InterruptedException e)
        {
            writer.println( e+" ID:14");
        }
        //-------------------------------------------------------------------                                                           
        try
        {
            desc.sendKeys(DESC);
            String Desc = "Description: "+DESC+" - Complete";
            writer.println(Desc+"\n");
            Thread.sleep(timer);
        }
        catch(InterruptedException e)
        {
            writer.println( e+" ID:15");
        }
        //-------------------------------------------------------------------
        try
        {
            inf_desc.sendKeys(INF_DESC);
            String Inf_Desc = "Inf_Des: "+INF_DESC+" - Complete";
            writer.println(Inf_Desc+"\n");
            Thread.sleep(timer);
        }
        catch(InterruptedException e)
        {
            writer.println( e+" ID:16");
        }
        //-------------------------------------------------------------------
        try
        {
            twt_url.sendKeys(TWT_URL);
            String Twt_Url= "URL: "+TWT_URL+" - Complete";
            writer.println(Twt_Url+"\n");
            Thread.sleep(timer);
        }
        catch(InterruptedException e)
        {
            writer.println( e+" ID:17");
        }
        //-------------------------------------------------------------------
        try
        {
            for(WebElement RadioOption : link_in_twt)
            {
                if(RadioOption.getAttribute("value").equals("website"))
                RadioOption.click();
                String Link_in_twt = "Radio in URL: Click - Complete";
                writer.println(Link_in_twt+"\n");
                Thread.sleep(timer);                                                                    
            }

        }
        catch(InterruptedException e)
        {
            writer.println( e+" ID:18");
        }
        //-------------------------------------------------------------------
        try
        {
            link_in_twt_1.click();
            String Link_in_twt_1  = "Radio in URL2: Click - Complete";
            writer.println(Link_in_twt_1+"\n");
            Thread.sleep(timer);
        }
        catch(InterruptedException e)
        {
            writer.println( e+" ID:19");
        }
        //-------------------------------------------------------------------
        try
        {
            link_in_twt_conf.click();
            String Link_in_twt_conf = "Radio URL3 Conf: Click - Complete";
            writer.println(Link_in_twt_conf+"\n");
            Thread.sleep(timer);
        }
        catch(InterruptedException e)
        {
            writer.println( e+" ID:20");
        }

        if(i != x)
        {
            //try
            //{
                report_another_twt.click();
                writer.println("Report Another: Click - Complete");
            //  Thread.sleep(3000);
            //}
            //catch(InterruptedException e)
            //{
            //  writer.println( e + "ID: Report Another x");
            //}                                                                 
        }
    }
    else
    {
        writer.println("There was an error in the MySQL query! ID:26");
    }


}

错误

java.lang.NullPointerException
at att.yet.petl.XmlServlet.doGet(XmlServlet.java:492)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

抱歉没有提供足够的信息

第 492 行是

WebElement ack1 = driver.findElement(By.id("claimer"));

protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {

    MysqlConnect();

    PrintWriter writer = response.getWriter();

        try
        { //note that the entire script is basically in one try/catch       
        int x =1;
        int i = 0;
        rs = st.executeQuery(" query is here");


            DesiredCapabilities capability = DesiredCapabilities.firefox();
            capability.setCapability("platform", Platform.ANY);
            capability.setCapability("binary", "/ms/dist/fsf/PROJ/firefox/16.0.0/bin/firefox");

            URL url2 = null;
            try {
                url2 = new URL("http://localhost:4444/wd/hub");
            } catch (MalformedURLException e1) {
                e1.printStackTrace();
            }

            WebDriver driver = new RemoteWebDriver(url2,capability);

            driver.get("https://site.com");

        WebDriverWait wait = new WebDriverWait(driver,10);

            int timer = 0;

                WebElement numOne = driver.findElement(By.id("owner")); 
                WebElement numTwo = driver.findElement(By.id("input_text")); 
                WebElement numThree = driver.findElement(By.id("comp"));

这之间的一切只是点击和sendKeys的尝试/捕获

4

1 回答 1

1

我猜这行会抛出 NullPointerException

if(RadioOption.getAttribute("value").equals("website"))

根据文档:http ://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebElement.html#getAttribute%28java.lang.String%29

java.lang.String getAttribute(java.lang.String name)
.........
返回: 属性的当前值,如果该值未设置,则返回null 。

如果未设置该属性,则该函数返回 null 并且整个表达式抛出 NullPionterException。

尝试这个:

if("website".equals(RadioOption.getAttribute("value"))
于 2013-10-03T22:39:08.913 回答