Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Selenium webdriver用于选择日期_Java_Selenium Webdriver - Fatal编程技术网

Java Selenium webdriver用于选择日期

Java Selenium webdriver用于选择日期,java,selenium-webdriver,Java,Selenium Webdriver,我是Selenium的新手,无法选择日期网站。 有人能帮我吗?我试图将值传递给只读文本框,但没有成功 public static void setup() throws Exception { System.out.println("Browser Set up start.. "); driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.manage().timeout

我是Selenium的新手,无法选择日期网站。 有人能帮我吗?我试图将值传递给只读文本框,但没有成功

public static void setup() throws Exception {
    System.out.println("Browser Set up start.. ");
    driver = new FirefoxDriver();
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
    driver.get("http://www.redbus.in/");
    System.out.println("Browser Set up Completed ");
}

@Test
public static void SelectSrcDest() throws Exception {
    System.out.println("Constructing Url to open");

    driver.findElement(By.id("txtSource")).sendKeys("Bangalore");
    driver.findElement(By.id("txtDestination")).sendKeys("Chennai");


    driver.findElement(By.xpath("html/body/div[2]/div/section/div[1]/img")).click();
    driver.findElement(By.xpath("html/body/div[2]/div/section/div[1]/img")).click();

    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("window.document.getElementById('txtOnwardCalendar').setAttribute('value','27-Mar-2014')");
    driver.findElement(By.xpath("html/body/div[2]/div/section/div[4]/button")).click();
}

public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub

    setup();
    SelectSrcDest();
}

可以使用xPath按文本查找元素,然后使用div id&table类选择要选择的行程和月份

例如,为了选择行程日期:

WebDriver driver = new FirefoxDriver();

driver.get("http://www.redbus.in");
driver.findElement(By.id("txtOnwardCalendar")).click();

By locator = By.xpath("//div[@id='rbcal_txtOnwardCalendar']" +
                      "/table[@class='monthTable first']" +
                      "//td[contains(text(), '10')]");
driver.findElement(locator).click();

将选择第一个月的第10天。如果您使用
monthTable last
作为课程,您将获得第二个月的第10天。如果您将div id更改为rbcal\u txtreurncalendar,则可以选择回程日期。

您可以尝试以下代码。它工作得很好

driver.findElement(By.id("DDLSource")).sendKeys("C");
    driver.findElement(By.xpath("//dl[@id = 'lis']//dt[text()='Chennai']")).click();
    driver.findElement(By.id("DDLDestination")).sendKeys("t");
    driver.findElement(By.xpath("//dl[@id = 'dis']//dt[text()='Theni']")).click();
    driver.findElement(By.className("calenImg")).click();
    driver.findElement(By.xpath("//td[text()='Feb']/../..//a[text()='17']")).click();
    driver.findElement(By.id("calendar1")).click();
    driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
    driver.findElement(By.xpath("//td[text()='Feb']/../..//a[text()='20']")).click();

请尝试以下操作以选择日期:

driver.findElement(By.id("give id")).click();
WebElement selectElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.className("ui-datepicker-year")));
Select select = new Select(selectElement);
select.selectByValue("2012");
Thread.sleep(6000);
WebElement dateWidget = driver.findElement(By.id("ui-monthpicker-div"));
List<WebElement> columns011=dateWidget011.findElements(By.tagName("td"));
for (WebElement cell: columns011){
//Select  Month
if (cell.getText().equals("Feb")){
cell.findElement(By.linkText("Feb")).click();
break;
}
} 
driver.findElement(By.id(“give id”))。单击();
WebElement selectElement=(新WebDriverWait(driver,10))。直到(ExpectedConditions.presenceOfElementLocated(By.className(“ui日期选择器年”));
选择=新选择(选择元素);
select.selectByValue(“2012”);
睡眠(6000);
WebElement dateWidget=driver.findElement(By.id(“ui-monthpicker-div”);
列表列011=dateWidget011.findElements(按.tagName(“td”));
for(WebElement单元格:columns011){
//选择月份
if(cell.getText()等于(“Feb”)){
cell.findElement(By.linkText(“Feb”))。单击();
打破
}
} 

尝试通过ID找出元素。。它很容易通过ID精确定位

driver.findElement(By.id("txtOnwardCalendar")).click();
WebElement selectElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.className("monthTable first")));
Select select = new Select(selectElement);
select.selectByValue("2012");
Thread.sleep(6000);
WebElement dateWidget = driver.findElement(By.id("monthTitle"));
List<WebElement> columns011=dateWidget011.findElements(By.tagName("td"));
for (WebElement cell: columns011){
//Select  Month
if (cell.getText().equals("Feb")){
cell.findElement(By.linkText("Feb")).click();
break;
}
}
driver.findElement(By.id(“txtForwardCalendar”))。单击();
WebElement selectElement=(新WebDriverWait(driver,10))。直到(ExpectedConditions.presenceOfElementLocated(By.className(“Monhtable first”));
选择=新选择(选择元素);
select.selectByValue(“2012”);
睡眠(6000);
WebElement dateWidget=driver.findElement(By.id(“monhtitle”);
列表列011=dateWidget011.findElements(按.tagName(“td”));
for(WebElement单元格:columns011){
//选择月份
if(cell.getText()等于(“Feb”)){
cell.findElement(By.linkText(“Feb”))。单击();
打破
}
}

这可能不是真的。如果它们只是位于页面外,那么这可能是真的,但如果它们的可见性设置为无或不透明度设置为0%,则无法通过这种方式单击。我在Selenium IDE上对其进行了测试,效果非常好,因此我认为它也适用于WebDriver。你会说WebDriver的行为不同吗?是的。WebDriver不允许您选择隐藏元素,因为它不通过javascript执行其命令。谢谢,我已更正。我应该知道的,但是。。。我想我是在偷懒。我在@MrTi发表评论后编辑了我的答案,以包含一个我测试过的工作示例,与页面相比,它对我来说运行良好。这是我以前用于Redbus站点的代码。但刚才我观察到,所有元素都已更改。你可以参考我的,自己编写脚本。谢谢,在更改元素后,我仍然无法使用webdriver选择特定的日期。谢谢Shymala它可以工作。。。!!!!!driver.findElement(By.id(“txtForwardCalendar”))。单击();findElement(By.xpath(“//div[@id='rbcal\u txtforwardcalendar']]/table/tbody/tr[8]/td”)。单击();线程“main”org.openqa.selenium.InvalidSelectorException中出现异常:不支持复合类名。考虑搜索一个类名并过滤结果或使用CSS选择器。谢谢您的代码,@阿米尔,上面的消息在执行您给出的代码后显示,请告知.Cuff.FunEngult(B.ID(“TxTunWaldCalaseLoad”))。findElement(By.xpath(“//div[@id='rbcal\u txtforwardcalendar']]/table/tbody/tr[8]/td”)。单击();阿米尔,谢谢你的建议,我遇到了一个例外:不支持复合类名。考虑搜索一个类名并过滤结果或使用CSS选择器。请建议我应该在我的代码中更正什么。这完全符合预期。如果类名包含空格,WebDriver会将其视为“复合选择器”。您可以删除By.className()定位器中的空格,该定位器仍然可以找到您要查找的元素;或者您可以使用类似于by.cssSelector(“.id”)的东西,通过CSS选择器进行查找,这为类似的功能提供了更大的灵活性。这正是异常消息所说的。