运行selenium代码时出错

时间:2014-09-13 20:57:10

标签: selenium selenium-rc

运行selenium代码时出现以下错误,而代码中的所有内容都正确。请帮忙

线程中的异常" main" com.thoughtworks.selenium.SeleniumException:ERROR:Element .//* [/ id =' content'] / p [2]未找到     在com.thoughtworks.selenium.HttpCommandProcessor.throwAssertionFailureExceptionOrError(HttpCommandProcessor.java:109)     在com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:103)     在com.thoughtworks.selenium.HttpCommandProcessor.getString(HttpCommandProcessor.java:272)     在com.thoughtworks.selenium.DefaultSelenium.getText(DefaultSelenium.java:471)     在selrcdemo.RCDemo.main(RCDemo.java:35)

package selrcdemo;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
public class RCDemo {

public static void main(String[] args) throws InterruptedException 
{
    Selenium selenium = new DefaultSelenium("localhost", 4444 , "firefox",  "http://www.calculator.net");
    selenium.start();   // Start
    selenium.open("/");  // Open the URL
    selenium.windowMaximize();

    // Click on Link Math Calculator
    selenium.click("xpath=.//*[@id='menu']/div[3]/a");
    Thread.sleep(2500); // Wait for page load

    // Click on Link Percent Calculator
    selenium.click("xpath=.//*[@id='menu']/div[4]/div[3]/a");
    Thread.sleep(4000); // Wait for page load


    // Focus on text Box
    selenium.focus("name=cpar1");
    // enter a value in Text box 1
    selenium.type("css=input[name=\"cpar1\"]", "10");

    // enter a value in Text box 2
    selenium.focus("name=cpar2");
    selenium.type("css=input[name=\"cpar2\"]", "50");

    // Click Calculate button
    selenium.click("xpath=.//*[@id='content']/table/tbody/tr/td[2]/input");
    Thread.sleep(4000);
    // verify if the result is 5
    String result = selenium.getText(".//*[@id='content']/p[2]");


    if (result == "5")
    {
        System.out.println("Pass");
    }else
    {
        System.out.println("Fail");
    }

    }
    }

5 个答案:

答案 0 :(得分:1)

如果你打开这个网站,你会发现输出没有存储在第三个文本框中,而是显示在上面,就在写入“Result”的地方。

您必须在代码中进行一些更改(单击按钮的xpath和第三个文本框的xpath,即从中获取值的位置)。以下代码完美无缺:

    package seleniumrcdemo;

    import com.thoughtworks.selenium.DefaultSelenium;
    import com.thoughtworks.selenium.Selenium;

    @SuppressWarnings("deprecation")
    public class rcdemo {
    public static void main(String[] args) throws InterruptedException {

  // Instatiate the RC Server
  Selenium selenium = new DefaultSelenium("localhost", 4444 , "firefox", "http://www.calculator.net");
  selenium.start();   // Start
  selenium.open("/");  // Open the URL
  selenium.windowMaximize();

  // Click on Link Math Calculator
  selenium.click("xpath=.//*[@id='menu']/div[3]/a");
  Thread.sleep(2500); // Wait for page load

  // Click on Link Percent Calculator
  selenium.click("xpath=.//*[@id='menu']/div[4]/div[3]/a");
  Thread.sleep(4000); // Wait for page load

  // Focus on text Box
  selenium.focus("name=cpar1");
  // enter a value in Text box 1
  selenium.type("css=input[name=\"cpar1\"]", "10");

  // enter a value in Text box 2
  selenium.focus("name=cpar2");
  selenium.type("css=input[name=\"cpar2\"]", "50");

  // Click Calculate button
  selenium.click("xpath=.//*[@id='content']/table[1]/tbody/tr[2]/td/input[2]");

  // verify if the result is 5
  Thread.sleep(4000);
  String result = selenium.getText("xpath=.//*[@id='content']/p[2]/font/b");
  //String result = selenium.getValue("xpath=.//*[@id='cpar3']");
  System.out.println("result"+result);

  if (result.equals("5")/*== "5"*/){
     System.out.println("Pass");
  }
  else{
     System.out.println("Fail");
  }
   }
}

答案 1 :(得分:0)

非常确定您需要在引号内添加xpath =。或者做:

String result = selenium.findElement(By.xpath('path')).getText();

你的答案(结果)在第二页中不存在。仔细检查后,xpath是:

/html/body/table[2]/tbody/tr/td[1]/div[1]/p[2]/span

答案 2 :(得分:0)

声明 字符串结果= selenium.getText(" .//* [@ id =' content'] / p [2]"); 错过" xpath ="。 正确的陈述

String result = selenium.getText("xpath=.//*[@id='content']/p[2]");

但这将返回结果" 50%= 5"的10%,当然!=" 5"。确切的xpath是元素" 5"是" xpath =。// * [@ id ='内容'] / p [2] / span / font / b"。所以声明应该是

String result = selenium.getText("xpath=.//*[@id='content']/p[2]/span/font/b");

你仍会得到"失败"在比较字符串文字。建议在比较字符串时使用equals()或compareTo()方法:

    if (result.equals("5"))
    {
        System.out.println("Pass");
    }else
    {
        System.out.println("Fail");
    }

希望这有帮助。

答案 3 :(得分:0)

    Thread.sleep(2000);
    // verify if the result is 5
    String result = selenium.getText("xpath=.//*[@id='content']/p[2]/span/font/b");

    if (result.equals("5")){
        System.out.println("Pass");
    }
    else{
        System.out.println("Fail");
    }

答案 4 :(得分:-1)

如果我错了,请纠正我

计算器web应用程序中的%计算模块未能通过测试用例,可能是应用程序代码需要修复bug,结果不显示在rhs(cpar3)端

xpath=.//*[@id='cpar3']==>RHS xpath

这里是selenium代码,如果我手动输入结果值为5

,它对我来说很好
package selRcDemo;

import com.thoughtworks.selenium.DefaultSelenium;

import com.thoughtworks.selenium.Selenium;

public class RcDemo {
    /**
     * @param args
     */


    public static void main(String[] args) throws InterruptedException {
        // Instatiate the RC Server
        Selenium selenium = new DefaultSelenium("localhost", 4444, "firefox",
                "http://www.calculator.net");
        selenium.start(); // Start
        selenium.open("/"); // Open the URL
        selenium.windowMaximize();

        // Click on Link Math Calculator
        selenium.click("xpath=.//*[@id='menu']/div[3]/a");
        Thread.sleep(2500); // Wait for page load

        // Click on Link Percent Calculator
        selenium.click("xpath=.//*[@id='menu']/div[4]/div[3]/a");
        Thread.sleep(4000); // Wait for page load

        // Focus on text Box
        selenium.focus("name=cpar1");
        // enter a value in Text box 1
        selenium.type("css=input[name=\"cpar1\"]", "10");

        // enter a value in Text box 2
        selenium.focus("name=cpar2");
        selenium.type("css=input[name=\"cpar2\"]", "50");

        // Click Calculate button
        selenium.click("xpath=.//*[@id='content']/table[1]/tbody/tr[2]/td/input[2]");
        Thread.sleep(2500);

        // verify if the result is 5
        String result = selenium.getText("xpath=.//*[@id='cpar3']");
        // result="5";
        System.out.println(result);
        // result is not equal to 5 due to bug in application code its returning
        // "" (no value)
        if (result.equals("5")) {
            System.out.println("Pass");
        } else {
            System.out.println("Fail");
        }
    }

}