【问题标题】:Screenscraping a Datepicker with Scrapy + Selenium使用 Scrapy + Selenium 截取日期选择器
【发布时间】:2016-04-05 18:06:05
【问题描述】:

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

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

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

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

【问题讨论】:

  • 是的,您正在寻找的可以完成,我可以在 selenium +java 中完成,但我不确定您所说的 Scrapy + Seleninum 是什么意思
  • 你能用Java发布代码吗?
  • 是的,请给我 15-30 分钟

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


【解决方案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");
        }

希望这对您也有帮助

【讨论】:

  • 谢谢,我会尽快测试,如果它有效,我会告诉你(标记你的答案是正确的)
  • 我想我快到了,但有时我会在检查元素是否是我想要单击的元素之前遇到这个问题StaleElementReferenceException: Message: Element not found in the cache - perhaps the page has changed since it was looked up,我也遇到NoSuchElementException: Message: Unable to locate element 时查找消息以检查其是否可用。顺便说一句,你没有添加点击更改月份吧?
  • 只是添加如何处理陈旧元素异常:您可以在签入和签出日期选择的for循环中看到上述代码(dateToBeSelected =“19”; datetobeselected =“27” ;) 我再次调用 alldates 只是为了避免过时的元素,所以你也可以在你的代码中这样做。
  • 这就是我的代码现在的样子dpaste.de/UDyD 它看起来和你的很相似,我添加了一些睡眠,看看我是否可以解决过时的问题,但仍然没有..你发现有什么问题吗?
  • 请准确告诉我您的代码中哪一行出现了陈旧的元素异常,这将有助于形成我的预期代码看起来不错
猜你喜欢
  • 2012-04-29
  • 2014-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-22
  • 2018-01-17
  • 1970-01-01
相关资源
最近更新 更多