【问题标题】:How to select check in and check out date using selenium in goibibo/hotels.com (Area Mumbai)如何在 goibibo/hotels.com(孟买地区)中使用 selenium 选择入住和退房日期
【发布时间】:2021-10-08 18:19:02
【问题描述】:

#如何在 goibibo/hotels.com(孟买地区)中使用 selenium 选择入住和退房日期# 假设我想选择入住日期 2021 年 8 月 25 日和退房日期 2021 年 9 月 3 日

https://www.goibibo.com/hotels/

package com.bean;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Goibibo {
    
    public static void main(String[] args) {
    System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver_2\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
     //Goto Url Goibibo.com
     driver.get("https://www.goibibo.com/hotels/");
     System.out.println("WWW.GOIBIBO.COM");
     
     //Select Country India
     driver.findElement(By.xpath("//input[@name='CountryType']")).click();

     System.out.println("Selected Country India");
     // Click on Search Bar
     driver.findElement(By.id("downshift-1-input")).click();

     System.out.println("Clicked On Search Bar");

     //Select Mumbai City
     WebDriverWait wait = new WebDriverWait(driver, 30);
     wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//p[text()='Mumbai']"))).click();
     System.out.println("Print Mumbai !");
     
     driver.findElement(By.id("search-widget-checkin-input")).click();
     System.out.println("Select Check In");
     
     }
}

【问题讨论】:

  • 您的代码在哪里?你试过什么?遇到了什么错误?
  • 您要选择哪个日期?您尝试过任何代码吗?任何错误堆栈跟踪?
  • 是的,我试过了,假设我想选择 21 August 2021 。我将添加代码

标签: selenium selenium-webdriver datepicker css-selectors selenium-chromedriver


【解决方案1】:

Wait = WebDriverWait(driver, 10)
From_Date = Wait.until(
    EC.visibility_of_all_elements_located(
        (
            By.XPATH,
            "//html[1]//body[1]//div[1]//section[1]//div[1]//div[1]//div[2]//div[2]//section[1]//div[1]//div[1]//div[2]//div[1]//ul[2]//li",
        )
    )
)
To_Date = Wait.until(
    EC.visibility_of_all_elements_located(
        (
            By.XPATH,
            "//html[1]//body[1]//div[1]//section[1]//div[1]//div[1]//div[2]//div[2]//section[1]//div[1]//div[2]//div[2]//div[1]//ul[2]//li",
        )
    )
)

for from_date in From_Date:
    print(from_date.text)
    if from_date.text == "20":
        from_date.click()
        break

for to_Date in To_Date:
    print(to_Date.text)
    if to_Date.text == "8":
        to_Date.click()
        break

【讨论】:

  • 嘿,我正在使用java语言,我认为EC在python中使用这样的来自selenium.webdriver.support import expected_conditions as ec
【解决方案2】:

您的某些 xpath 似乎错误

 //Select Country India
 //This returns both radio buttons
 driver.findElement(By.xpath("//input[@name='CountryType']")).click();

变成这样

driver.findElement(By.xpath("//*[text()='India']")).click();

其余代码似乎可用于打开签入日期选择器

之后,您可以像这样选择入住和退房日期:

用于在左侧日期选择器中选择第 25 个日期

driver.findElement(By.xpath("//div[contains(@class, "dcalendar-newstyles__DayAndDateLeftWrapper")]//ul[contains(@class, "dcalendar-newstyles__DateWrapDiv")]//*[text()='25']")).click();

用于在正确的日期选择器中选择第三个日期

driver.findElement(By.xpath("//div[contains(@class, "dcalendar-newstyles__DayAndDateRightWrapper")]//ul[contains(@class, "dcalendar-newstyles__DateWrapDiv")]//*[text()='3']")).click();

我知道那些 xpath 非常糟糕。但是由于您提供的网站包含带有太多随机字母和数字的定位器,因此我没有看到任何独特的定位器,例如 id。所以我找不到更好的方法。

【讨论】:

  • 其实 xpath 很糟糕 (: ,not working
  • 我已经尝试了那些带有 chropath 扩展的 xpath,它们正在工作。你得到错误还是什么都不做?也许在打开日期选择器后添加简短的 Thread.sleep 会有所帮助。
  • 在 dcalendar, 标记 "dcalendar" 上的语法错误,删除此标记错误发生在 /ul[contains(@class, Syntax error on token "")]//ul[contains(@class , "", -> 发生预期错误
猜你喜欢
  • 2020-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多