处理硒中的日历

时间:2019-04-01 04:15:11

标签: selenium selenium-webdriver

无论我的代码何时执行,它都会导航到其他页面。我的代码是如何处理硒中的日历。 请帮助

package basic;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class calender {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        WebDriver driver = new ChromeDriver();
        //Launching website
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        driver.get("https://www.path2usa.com/travel-companions");
        //selecting dates
        driver.findElement(By.xpath("//*[@id=\"travel_date\"]")).click();


        while(!driver.findElement(By.cssSelector("[class='datepicker-days'] th[class='datepicker-switch']")).getText().contains("April"))
        {
            driver.findElement(By.cssSelector("[class='datepicker-days'] th[class='next']")).click();

        }

        List<WebElement> dates = driver.findElements(By.className("day"));
        //grab common attribute // put into list and iterate
        int count = driver.findElements(By.className("day")).size();
        for(int i=0;i<count;i++)
        {
            String text = driver.findElements(By.className("day")).get(i).getText();
            if(text.equalsIgnoreCase("23"))
            {
                driver.findElements(By.className("day")).get(i).click();
                break;
            }
        }

    }

}

starting ChromeDriver 73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72) on port 13761
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
Apr 01, 2019 9:38:18 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Cannot locate an element using css selector=[class='datepicker-days'] th[class='datepicker-switch']
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'Nilufars-MacBook-Air.local', ip: '2405:204:4383:7327:1104:ad36:576:9d64%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.12.6', java.version: '1.8.0_201'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:327)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:420)
    at org.openqa.selenium.By$ByCssSelector.findElement(By.java:431)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
    at basic.calender.main(calender.java:23)

1 个答案:

答案 0 :(得分:1)

欢迎来到SO。 您可以直接选择日期,如下所示。

driver.findElement(By.xpath("//input[@name='travel_date']")).sendKeys("25 May 2019");

通过这种方式,您将忽略月循环和日期循环来节省执行时间。

简化代码:

driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.get("https://www.path2usa.com/travel-companions");
//selecting the date
driver.findElement(By.xpath("//input[@name='travel_date']")).sendKeys("25 May 2020");
//continue your test with next steps

// quit the driver.
driver.quit();