尝试在外部设置日期属性,但是找不到源代码异常

时间:2014-12-04 05:42:14

标签: selenium datepicker

我试图在外部更改日期选择器的日期值,但是它的显示源未找到异常。请帮助。

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;


public class datepicker2 {
     WebDriver driver=null;
    @Test
    public void makemytrip(){
        System.setProperty("webdriver.ie.driver","C:\\Users\\sudharshan.r\\Downloads\\IEDriverServer.exe");    
      driver = new InternetExplorerDriver();
      String baseUrl = "http://www.sugamatourists.com/";
       driver.get(baseUrl);

       WebElement element1 = driver.findElement(By.cssSelector("#searchbus_depart"));
        setAttribute(element1,"value","30/12/2014");

        driver.findElement(By.id("search_submit_btn")).click();



      }
      public void setAttribute(WebElement element, String attName, String attValue) {
          JavascriptExecutor js = (JavascriptExecutor)driver;
          js.executeScript("arguments[0].setAttribute(arguments[1], arguments[2]);",element, attName, attValue);
      }

}

抛出的异常看起来像......

// Compiled from ReflectiveCallable.java (version 1.5 : 49.0, super bit)
public abstract class org.junit.internal.runners.model.ReflectiveCallable {

  // Method descriptor #8 ()V
  // Stack: 1, Locals: 1
  public ReflectiveCallable();
    0  aload_0 [this]
    1  invokespecial java.lang.Object() [1]
    4  return
      Line numbers:
        [pc: 0, line: 9]
      Local variable table:
        [pc: 0, pc: 5] local: this index: 0 type: org.junit.internal.runners.model.ReflectiveCallable

在外部我的意思是改变HTML代码本身。

<input id="searchbus_depart" class="hasDatepicker" type="text" onkeypress="return handleEnter(this, event);" style="width:70px" size="12" value="30/12/2014" readonly="1" name="searchbus[depart]">

使用

更改HTML中的value="30/12/2014"
 WebElement element1 = driver.findElement(By.cssSelector("#searchbus_depart"));
            setAttribute(element1,"value","30/12/2014");

 public void setAttribute(WebElement element, String attName, String attValue) {
              JavascriptExecutor js = (JavascriptExecutor)driver;
              js.executeScript("arguments[0].setAttribute(arguments[1], arguments[2]);",element, attName, attValue);
          }

1 个答案:

答案 0 :(得分:0)

以下代码对我有用。我正在使用 Selenium 2.44.0,junit 4和Firefox 31.2.0 ESR 。请检查它是否也适用于您:

import java.util.concurrent.TimeUnit;

import org.junit.Test;
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;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Testing{

    WebDriver driver = null;

    @Test
    public void makemytrip(){
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

        String baseUrl = "http://www.sugamatourists.com/";
        driver.get(baseUrl); //Navigating to the site

        //Closing the popup window that comes up
        try{
            WebDriverWait wait = new WebDriverWait(driver, 10);
            WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='light']/a")));
            element.click();
        }catch(Throwable e){
            System.err.println("Close button not found to click upon. "+e.getMessage());
        }

        //Setting the value of the Date field 'Departing on'
        WebElement element1 = driver.findElement(By.cssSelector("#searchbus_depart"));
        setAttribute(element1,"value","24/11/2015");

        driver.findElement(By.id("search_submit_btn")).click();

    }
    public void setAttribute(WebElement element, String attName, String attValue) {
        JavascriptExecutor js = (JavascriptExecutor)driver;
        js.executeScript("arguments[0].setAttribute(arguments[1], arguments[2]);",element, attName, attValue);
    }
}