首页 文章

如何使用selenium webdriver从预订网站的日历中选择今天和明天的两个日期

提问于
浏览
-3

Screenshot from the calender

我尝试下面的代码来检测chekin按钮,但我无法确定如何选择日期

公共类Hotel_Search {void search(WebDriver driver){//查找目标WebElement des = driver.findElement(By.name(“ss”)); //填写目的地des.sendKeys(“Ain Sokhna”); //选择签入按钮WebElement Checkinhbutton = driver.findElement(By.xpath(“html / body / div [3] / div / div / div [2] / form / div1 / div [2] / div / div [2] / DIV / DIV / DIV / DIV1 / DIV /按钮“)); Checkinhbutton.click(); //调用今天方法的选择日期SelectDateOfToday(driver.findElement(By.xpath(“html / body / div [3] / div / div / div [2] / form / div1 / div [2] / div / div [ 2] / DIV / DIV / DIV / DIV [2] / DIV [2] / DIV [3] / DIV / DIV / DIV1 /表/ THEAD / TR1 /日“))); //找到搜索按钮WebElement searchbutton = driver.findElement(By.xpath(“html / body / div [3] / div / div / div [2] / form / div1 / div [4] / div [2] / button “)); searchbutton.click(); } public void SelectDateOfToday(WebElement Calender_Xpath){

String today = getCurrentDay();
        List<WebElement> columns = Calender_Xpath.findElements(By.tagName("td"));
        for (WebElement cell : columns) {
                       /*
                       * //If you want to click 18th Date if (cell.getText().equals("18")) {
                       */
                       // Select Today's Date
                       if (cell.getText().equals(today)) {
                                      cell.click();
                                      break;
                       }
        }         

        }
    private String getCurrentDay() {
         DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate localDate = LocalDate.now();
            String date = dtf.format(localDate);
            return date;

    }

}

1 回答

  • 0

    正如我从你的问题中所理解的那样,你正试图与https://www.booking.com/进行交互 . 为了能够选择日期,您可以尝试以下方法:

    import com.google.common.base.Function;
    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.List;
    import java.util.Locale;
    
    public class BookingTest {
      public static void main(String[] args) throws InterruptedException {
        // Create a new instance of the Firefox driver
        // Notice that the remainder of the code relies on the interface,
        // not the implementation.
        final WebDriver driver = new ChromeDriver();
    
        // And now use this to visit Google
        driver.get("https://www.booking.com/");
        // wait until page will load completely
        new WebDriverWait(driver, 10).until(
            (Function<? super WebDriver, Boolean>) webDriver -> ((JavascriptExecutor) driver)
                .executeScript("return document.readyState").equals("complete"));
    
        // open calendar
        // note sometimes the xpath to the clendar button gets changed, therefore is the 'if' statement here
        List<WebElement> openCalButton = driver.findElements(By.xpath("//button[@aria-label= 'Open calendar']"));
        if (openCalButton.size() < 1){
          openCalButton = driver.findElements(By.xpath("//span[@class= 'sb-date-field__icon sb-date-field__icon-btn bk-svg-wrapper calendar-restructure-sb']"));
        }
        openCalButton.get(0).click();
    
        // store month and year
        Calendar cal=Calendar.getInstance();
        SimpleDateFormat month_date = new SimpleDateFormat("MMMM", Locale.US);
        String month_name = month_date.format(cal.getTime());
        String year = Calendar.getInstance().get(Calendar.YEAR) + "";
        String monthYear = month_name + " " + year;
    
        // store day
        String day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH) + "";
    
        // find date
        WebElement date =  driver.findElements(By.xpath("//th[contains(.,'"
            + monthYear + "')]/parent::*/parent::*/parent::*/tbody/tr/td/span[@class = 'c2-day-inner'][contains(.,'"
            + day + "')]")).get(0);
        date.click();
    
        // this pause is only to prevent to exit the code immediately, if you want to see yhe result before page closes
        Thread.sleep(3000);
        driver.quit();
      }
    }
    

    输出:

    enter image description here

相关问题