3

I have been trying to get property listings data from a website where sometimes there is a 'View More' button to load more results and sometimes it is not there. In the first case, I have been using the following code and it works fine. For example,

  url<- "http://www.magicbricks.com/property-for-rent/residential-real-estate?bedroom=2&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment,Service-Apartment,Residential-House,Villa&cityName=Navi-Mumbai&Locality=Vashi"
  remDr$navigate(url)
  Sys.sleep(3)
  webElem <- remDr$findElement("css", "#viewMoreButton a")
  while(webElem$isElementDisplayed()[[1]]){
    tryCatch({
      Sys.sleep(1)
      webElem <- remDr$findElement("css", "#viewMoreButton a")
      webElem$clickElement()
    }, error=function(e){})
  }
  mb<- read_html(remDr$getPageSource()[[1]])

But in the second case, it doesn't work. For example:

  url<- "http://www.magicbricks.com/property-for-rent/residential-real-estate?bedroom=4&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment,Service-Apartment,Residential-House,Villa&cityName=Navi-Mumbai&Locality=Vashi"
  remDr$navigate(url)
  Sys.sleep(3)
  webElem <- remDr$findElement("css", "#viewMoreButton a")
  while(webElem$isElementDisplayed()[[1]]){
    tryCatch({
      Sys.sleep(1)
      webElem <- remDr$findElement("css", "#viewMoreButton a")
      webElem$clickElement()
    }, error=function(e){})
  }
  mb<- read_html(remDr$getPageSource()[[1]])

This code gets stuck at the line 'WebElem<- renDr$findElement()' and doesn't move forward or throw error.

What am I doing wrong?

Nadeem Hussain
  • 219
  • 4
  • 16
  • In the 2nd example there is no viewMoreButton to click on the page – jdharrison Nov 02 '16 at 00:26
  • @jdharrison yes, so shouldn't it skip the code inside the while loop and go on to read the page in this case? – Nadeem Hussain Nov 02 '16 at 08:24
  • Running your code I get: `Error: Summary: NoSuchElement` when I run `webElem <- remDr$findElement("css", "#viewMoreButton a")` . The while loop never executes giving error `Error: object 'webElem' not found` as expected as webElem was never assigned. – jdharrison Nov 02 '16 at 10:35

1 Answers1

0

Adding an if condition solves the problem:

  url<- "http://www.magicbricks.com/property-for-rent/residential-real-estate?bedroom=4&proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment,Service-Apartment,Residential-House,Villa&cityName=Navi-Mumbai&Locality=Vashi"
  remDr$navigate(url)
  Sys.sleep(3)
  if(length(remDr$findElements("css", "#viewMoreButton a"))!=0){
    while(webElem$isElementDisplayed()[[1]]){
      tryCatch({
      Sys.sleep(1)
      webElem <- remDr$findElement("css", "#viewMoreButton a")
      webElem$clickElement()
      }, error=function(e){})
    }
  }
  mb<- read_html(remDr$getPageSource()[[1]])
Nadeem Hussain
  • 219
  • 4
  • 16