用Scrapy + Selenium筛选一个Datepicker

时间:2016-04-05 18:06:05

标签: selenium selenium-webdriver datepicker scrapy screen-scraping

所以我需要删除像this for examplethis这样的页面,我正在使用Scrapy + Seleninum与日期选择器日历进行交互。

基本上我需要检查一个月内每天房间的可用性,所以我的想法是尝试点击日期选择器上未禁用的链接/日期,并检查是否出现错误消息知道它是否可用。

我不知道这是否是正确的方法,甚至不知道如何使用这些工具。我一直在和他们一起玩,但看起来我还没有达到最终的解决方案。

有谁知道我如何解决这个问题,甚至为我提供实现此目的的代码?

1 个答案:

答案 0 :(得分:1)

您好,请在下面找到答案

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("https://www.airbnb.pt/rooms/516301?check_in=2016-04-14&guests=1&check_out=2016-04-17");

// selecting firstdate picker -- check in

driver.findElement(By.xpath("//*[@class='col-sm-6']/input")).click();
// note calendar is not completely visible hence to make it visible 
// scroll a little bit down
((JavascriptExecutor) driver).executeScript("window.scrollBy(0,200)");
// take all calendar dates inside the list
// here i have update my code to skip stale element exception 
List<WebElement> alldates = driver.findElements(By.xpath("//*[@class='ui-datepicker-calendar']/tbody/tr/td/a[contains(@class, 'ui-state-default')]"));
System.out.println("Size is : "+ alldates.size());
// suppose you want to select 27 form the all dates
// keeping it as a parameter 
String dateToBeSelected = "19";  // check in
for(int i=0;i<alldates.size();i++){
    alldates = driver.findElements(By.xpath("//*[@class='ui-state-default']"));
System.out.println("dates is : " + alldates.get(i).getText());
// selects a check-in date
if( alldates.get(i).getText().equals(dateToBeSelected)){
    alldates.get(i).click();
    break;
            }
        }
// on selection of Checkin date check out calender automatically pop ups
System.out.println("--------------------------");
String datetobeselected = "27";
for(int i=0;i<alldates.size();i++){
    alldates = driver.findElements(By.xpath("//*[@class='ui-state-default']"));
System.out.println("dates is : " + alldates.get(i).getText());
// selects a check-in date
if( alldates.get(i).getText().equals(datetobeselected)){
    alldates.get(i).click();
    break;
            }
        }
// actually after calendar selection whatever text is shown in red color
// i am not sure what is it (sorry only English)

// identify the text with red color is present or not 
boolean errorMsg = driver.findElement(By.xpath("//*[@class='panel-body panel-light']/div[2]/p")).isDisplayed();
if(errorMsg){
    System.out.println("error msg is displayed");
}else{
System.out.println("error msg is not - displayed");
        }

希望这有助于您注意上面的示例,如果您希望代码自动取消禁用日期,请将签入和结帐日期作为参数,请随时询问我是否会更新我的答案。