Looping in RSelenium and Scraping












0














I'm trying to scrape data from website using RSelenium. I am able to navigate through drop downs individually but when I run them in loop I get error.



Also after selecting all the values in the drop down I want to store the name of the facility and contact details in a table. Which I'm not able to do so far.



rm(list=ls())
setwd("D:\work_codes\kvk\data")
getwd()

library(RSelenium)
library(rvest)
library(XML)
library(RCurl)
library(magrittr)
library(stringr)

rd<-rsDriver()
remDr<-rd[["client"]]

remDr$navigate("https://kvk.icar.gov.in/facilities_list.aspx")

remDr$refresh()

stateEle<-remDr$findElement("id", "ContentPlaceHolder1_ddlState")
states<-stateEle$getElementText()[[1]] %>% strsplit(., '\n') %>% unlist %>% setdiff(., ' --Select--')
states<-str_trim(states, 'left')
stateEle$clickElement()

for (i in 1:length(states)) {
remDr$refresh()
stateEle$clickElement()
stateEle$sendKeysToElement(list(states[i]))
stateEle$clickElement()
districts<-NULL
distEle<-remDr$findElement("id", "ContentPlaceHolder1_ddlDistrict")
districts<-distEle$getElementText()[[1]] %>% strsplit(., '\n') %>% unlist %>% setdiff(., ' --Select--')
districts<-str_trim(districts, 'left')
for (j in 1:length(districts)) {
distEle$clickElement()
distEle$sendKeysToElement(list(districts[j]))
distEle$clickElement()
kvk<-NULL
kvkEle<-remDr$findElement("id", "ContentPlaceHolder1_ddlKvk")
kvk<-kvkEle$getElementText()[[1]] %>% strsplit(., '\n') %>% unlist %>% setdiff(., ' --Select--')
kvk<-str_trim(kvk, 'left')
for (k in 1:length(kvk)) {
kvkEle$clickElement()
kvkEle$sendKeysToElement(list(kvk[[1]]))
kvkEle$clickElement()
submitEle<-remDr$findElement("id", "ContentPlaceHolder1_btnSubmit")
submitEle$clickElement()
doc<-remDr$findElement('id', 'ContentPlaceHolder1_rptfacility_f_name_1')
doc$getElementText()
doc$clickElement()
remDr$findElement('class name','Contact details:')
}
}
}









share|improve this question





























    0














    I'm trying to scrape data from website using RSelenium. I am able to navigate through drop downs individually but when I run them in loop I get error.



    Also after selecting all the values in the drop down I want to store the name of the facility and contact details in a table. Which I'm not able to do so far.



    rm(list=ls())
    setwd("D:\work_codes\kvk\data")
    getwd()

    library(RSelenium)
    library(rvest)
    library(XML)
    library(RCurl)
    library(magrittr)
    library(stringr)

    rd<-rsDriver()
    remDr<-rd[["client"]]

    remDr$navigate("https://kvk.icar.gov.in/facilities_list.aspx")

    remDr$refresh()

    stateEle<-remDr$findElement("id", "ContentPlaceHolder1_ddlState")
    states<-stateEle$getElementText()[[1]] %>% strsplit(., '\n') %>% unlist %>% setdiff(., ' --Select--')
    states<-str_trim(states, 'left')
    stateEle$clickElement()

    for (i in 1:length(states)) {
    remDr$refresh()
    stateEle$clickElement()
    stateEle$sendKeysToElement(list(states[i]))
    stateEle$clickElement()
    districts<-NULL
    distEle<-remDr$findElement("id", "ContentPlaceHolder1_ddlDistrict")
    districts<-distEle$getElementText()[[1]] %>% strsplit(., '\n') %>% unlist %>% setdiff(., ' --Select--')
    districts<-str_trim(districts, 'left')
    for (j in 1:length(districts)) {
    distEle$clickElement()
    distEle$sendKeysToElement(list(districts[j]))
    distEle$clickElement()
    kvk<-NULL
    kvkEle<-remDr$findElement("id", "ContentPlaceHolder1_ddlKvk")
    kvk<-kvkEle$getElementText()[[1]] %>% strsplit(., '\n') %>% unlist %>% setdiff(., ' --Select--')
    kvk<-str_trim(kvk, 'left')
    for (k in 1:length(kvk)) {
    kvkEle$clickElement()
    kvkEle$sendKeysToElement(list(kvk[[1]]))
    kvkEle$clickElement()
    submitEle<-remDr$findElement("id", "ContentPlaceHolder1_btnSubmit")
    submitEle$clickElement()
    doc<-remDr$findElement('id', 'ContentPlaceHolder1_rptfacility_f_name_1')
    doc$getElementText()
    doc$clickElement()
    remDr$findElement('class name','Contact details:')
    }
    }
    }









    share|improve this question



























      0












      0








      0







      I'm trying to scrape data from website using RSelenium. I am able to navigate through drop downs individually but when I run them in loop I get error.



      Also after selecting all the values in the drop down I want to store the name of the facility and contact details in a table. Which I'm not able to do so far.



      rm(list=ls())
      setwd("D:\work_codes\kvk\data")
      getwd()

      library(RSelenium)
      library(rvest)
      library(XML)
      library(RCurl)
      library(magrittr)
      library(stringr)

      rd<-rsDriver()
      remDr<-rd[["client"]]

      remDr$navigate("https://kvk.icar.gov.in/facilities_list.aspx")

      remDr$refresh()

      stateEle<-remDr$findElement("id", "ContentPlaceHolder1_ddlState")
      states<-stateEle$getElementText()[[1]] %>% strsplit(., '\n') %>% unlist %>% setdiff(., ' --Select--')
      states<-str_trim(states, 'left')
      stateEle$clickElement()

      for (i in 1:length(states)) {
      remDr$refresh()
      stateEle$clickElement()
      stateEle$sendKeysToElement(list(states[i]))
      stateEle$clickElement()
      districts<-NULL
      distEle<-remDr$findElement("id", "ContentPlaceHolder1_ddlDistrict")
      districts<-distEle$getElementText()[[1]] %>% strsplit(., '\n') %>% unlist %>% setdiff(., ' --Select--')
      districts<-str_trim(districts, 'left')
      for (j in 1:length(districts)) {
      distEle$clickElement()
      distEle$sendKeysToElement(list(districts[j]))
      distEle$clickElement()
      kvk<-NULL
      kvkEle<-remDr$findElement("id", "ContentPlaceHolder1_ddlKvk")
      kvk<-kvkEle$getElementText()[[1]] %>% strsplit(., '\n') %>% unlist %>% setdiff(., ' --Select--')
      kvk<-str_trim(kvk, 'left')
      for (k in 1:length(kvk)) {
      kvkEle$clickElement()
      kvkEle$sendKeysToElement(list(kvk[[1]]))
      kvkEle$clickElement()
      submitEle<-remDr$findElement("id", "ContentPlaceHolder1_btnSubmit")
      submitEle$clickElement()
      doc<-remDr$findElement('id', 'ContentPlaceHolder1_rptfacility_f_name_1')
      doc$getElementText()
      doc$clickElement()
      remDr$findElement('class name','Contact details:')
      }
      }
      }









      share|improve this question















      I'm trying to scrape data from website using RSelenium. I am able to navigate through drop downs individually but when I run them in loop I get error.



      Also after selecting all the values in the drop down I want to store the name of the facility and contact details in a table. Which I'm not able to do so far.



      rm(list=ls())
      setwd("D:\work_codes\kvk\data")
      getwd()

      library(RSelenium)
      library(rvest)
      library(XML)
      library(RCurl)
      library(magrittr)
      library(stringr)

      rd<-rsDriver()
      remDr<-rd[["client"]]

      remDr$navigate("https://kvk.icar.gov.in/facilities_list.aspx")

      remDr$refresh()

      stateEle<-remDr$findElement("id", "ContentPlaceHolder1_ddlState")
      states<-stateEle$getElementText()[[1]] %>% strsplit(., '\n') %>% unlist %>% setdiff(., ' --Select--')
      states<-str_trim(states, 'left')
      stateEle$clickElement()

      for (i in 1:length(states)) {
      remDr$refresh()
      stateEle$clickElement()
      stateEle$sendKeysToElement(list(states[i]))
      stateEle$clickElement()
      districts<-NULL
      distEle<-remDr$findElement("id", "ContentPlaceHolder1_ddlDistrict")
      districts<-distEle$getElementText()[[1]] %>% strsplit(., '\n') %>% unlist %>% setdiff(., ' --Select--')
      districts<-str_trim(districts, 'left')
      for (j in 1:length(districts)) {
      distEle$clickElement()
      distEle$sendKeysToElement(list(districts[j]))
      distEle$clickElement()
      kvk<-NULL
      kvkEle<-remDr$findElement("id", "ContentPlaceHolder1_ddlKvk")
      kvk<-kvkEle$getElementText()[[1]] %>% strsplit(., '\n') %>% unlist %>% setdiff(., ' --Select--')
      kvk<-str_trim(kvk, 'left')
      for (k in 1:length(kvk)) {
      kvkEle$clickElement()
      kvkEle$sendKeysToElement(list(kvk[[1]]))
      kvkEle$clickElement()
      submitEle<-remDr$findElement("id", "ContentPlaceHolder1_btnSubmit")
      submitEle$clickElement()
      doc<-remDr$findElement('id', 'ContentPlaceHolder1_rptfacility_f_name_1')
      doc$getElementText()
      doc$clickElement()
      remDr$findElement('class name','Contact details:')
      }
      }
      }






      for-loop rvest rselenium






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday









      Bharath

      864514




      864514










      asked yesterday









      Paritosh Sharma

      74




      74
























          1 Answer
          1






          active

          oldest

          votes


















          0














          library(rvest)
          url<-"https://kvk.icar.gov.in/facilities_list.aspx"

          page<-html_session(url)
          form<-html_form(page)[[1]]

          states<-html_nodes(page,css="#ContentPlaceHolder1_ddlState > option") %>% html_attr("value")
          states<-states[-1]
          states_name<-html_nodes(page,css="#ContentPlaceHolder1_ddlState > option") %>% html_text()
          states_name<-states_name[-1]

          final_df<-0
          #### STATES LOOP ####
          for(i in 1:length(states)){
          filled_form<-set_values(form,
          "ctl00$ContentPlaceHolder1$ddlState"=states[i])
          page1<-submit_form(page,filled_form)
          district<-html_nodes(page1,css="#ContentPlaceHolder1_ddlDistrict > option") %>% html_attr("value")
          district<-district[-1]
          district_name<-html_nodes(page1,css="#ContentPlaceHolder1_ddlDistrict > option") %>% html_text()
          district_name<-district_name[-1]

          #### DISTRICT LOOP ####
          for(j in 1:length(district)){
          filled_form1<-set_values(html_form(page1)[[1]],
          "ctl00$ContentPlaceHolder1$ddlState"=states[i],
          "ctl00$ContentPlaceHolder1$ddlDistrict"=district[j])
          page2<-submit_form(page1,filled_form1)
          kvk<-html_nodes(page2,css="#ContentPlaceHolder1_ddlKvk > option") %>% html_attr("value")
          kvk<-kvk[-1]
          kvk_name<-html_nodes(page2,css="#ContentPlaceHolder1_ddlKvk > option") %>% html_text()
          kvk_name<-kvk_name[-1]

          #### KVK LOOP ####
          for(k in 1:length(kvk)){
          filled_form2<-set_values(html_form(page2)[[1]],
          "ctl00$ContentPlaceHolder1$ddlState"=states[i],
          "ctl00$ContentPlaceHolder1$ddlDistrict"=district[j],
          "ctl00$ContentPlaceHolder1$ddlKvk"=kvk[k])
          page3<-submit_form(page2,filled_form2)
          contact_text<-gsub("[rn]","",html_nodes(page3,css=".panel-body") %>% html_text())
          if(length(contact_text) == 0){contact_text=""}
          df<-data.frame(cbind(states_name[i],district_name[j],kvk[k],contact_text))
          names(df)<-c("STATE","DISTRICT","KVK","CONTACT_TEXT")
          final_df[i*j*k] = list(df)
          ### WAITTIME TO AVOID HTTP 500 error - So the server is not overloaded
          sleep(5)
          }
          }
          }


          output_df<-data.table::rbindlist(final_df,fill=TRUE)

          # After this perform some string operations to extract the exact information required from the CONTACT_TEXT variable


          The above answer does not use any RSelenium package and I think this is more trustworthy than RSelenium.






          share|improve this answer





















          • Hi @Bharath This works perfectly except that after scraping 94th observation it stops and throws this error: Error in read_xml.response(x$response, ..., as_html = as_html) : Internal Server Error (HTTP 500). In addition: Warning message: In request_POST(session, url = url, body = request$values, encode = request$encode, : Internal Server Error (HTTP 500).
            – Paritosh Sharma
            1 hour ago











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53943923%2flooping-in-rselenium-and-scraping%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          library(rvest)
          url<-"https://kvk.icar.gov.in/facilities_list.aspx"

          page<-html_session(url)
          form<-html_form(page)[[1]]

          states<-html_nodes(page,css="#ContentPlaceHolder1_ddlState > option") %>% html_attr("value")
          states<-states[-1]
          states_name<-html_nodes(page,css="#ContentPlaceHolder1_ddlState > option") %>% html_text()
          states_name<-states_name[-1]

          final_df<-0
          #### STATES LOOP ####
          for(i in 1:length(states)){
          filled_form<-set_values(form,
          "ctl00$ContentPlaceHolder1$ddlState"=states[i])
          page1<-submit_form(page,filled_form)
          district<-html_nodes(page1,css="#ContentPlaceHolder1_ddlDistrict > option") %>% html_attr("value")
          district<-district[-1]
          district_name<-html_nodes(page1,css="#ContentPlaceHolder1_ddlDistrict > option") %>% html_text()
          district_name<-district_name[-1]

          #### DISTRICT LOOP ####
          for(j in 1:length(district)){
          filled_form1<-set_values(html_form(page1)[[1]],
          "ctl00$ContentPlaceHolder1$ddlState"=states[i],
          "ctl00$ContentPlaceHolder1$ddlDistrict"=district[j])
          page2<-submit_form(page1,filled_form1)
          kvk<-html_nodes(page2,css="#ContentPlaceHolder1_ddlKvk > option") %>% html_attr("value")
          kvk<-kvk[-1]
          kvk_name<-html_nodes(page2,css="#ContentPlaceHolder1_ddlKvk > option") %>% html_text()
          kvk_name<-kvk_name[-1]

          #### KVK LOOP ####
          for(k in 1:length(kvk)){
          filled_form2<-set_values(html_form(page2)[[1]],
          "ctl00$ContentPlaceHolder1$ddlState"=states[i],
          "ctl00$ContentPlaceHolder1$ddlDistrict"=district[j],
          "ctl00$ContentPlaceHolder1$ddlKvk"=kvk[k])
          page3<-submit_form(page2,filled_form2)
          contact_text<-gsub("[rn]","",html_nodes(page3,css=".panel-body") %>% html_text())
          if(length(contact_text) == 0){contact_text=""}
          df<-data.frame(cbind(states_name[i],district_name[j],kvk[k],contact_text))
          names(df)<-c("STATE","DISTRICT","KVK","CONTACT_TEXT")
          final_df[i*j*k] = list(df)
          ### WAITTIME TO AVOID HTTP 500 error - So the server is not overloaded
          sleep(5)
          }
          }
          }


          output_df<-data.table::rbindlist(final_df,fill=TRUE)

          # After this perform some string operations to extract the exact information required from the CONTACT_TEXT variable


          The above answer does not use any RSelenium package and I think this is more trustworthy than RSelenium.






          share|improve this answer





















          • Hi @Bharath This works perfectly except that after scraping 94th observation it stops and throws this error: Error in read_xml.response(x$response, ..., as_html = as_html) : Internal Server Error (HTTP 500). In addition: Warning message: In request_POST(session, url = url, body = request$values, encode = request$encode, : Internal Server Error (HTTP 500).
            – Paritosh Sharma
            1 hour ago
















          0














          library(rvest)
          url<-"https://kvk.icar.gov.in/facilities_list.aspx"

          page<-html_session(url)
          form<-html_form(page)[[1]]

          states<-html_nodes(page,css="#ContentPlaceHolder1_ddlState > option") %>% html_attr("value")
          states<-states[-1]
          states_name<-html_nodes(page,css="#ContentPlaceHolder1_ddlState > option") %>% html_text()
          states_name<-states_name[-1]

          final_df<-0
          #### STATES LOOP ####
          for(i in 1:length(states)){
          filled_form<-set_values(form,
          "ctl00$ContentPlaceHolder1$ddlState"=states[i])
          page1<-submit_form(page,filled_form)
          district<-html_nodes(page1,css="#ContentPlaceHolder1_ddlDistrict > option") %>% html_attr("value")
          district<-district[-1]
          district_name<-html_nodes(page1,css="#ContentPlaceHolder1_ddlDistrict > option") %>% html_text()
          district_name<-district_name[-1]

          #### DISTRICT LOOP ####
          for(j in 1:length(district)){
          filled_form1<-set_values(html_form(page1)[[1]],
          "ctl00$ContentPlaceHolder1$ddlState"=states[i],
          "ctl00$ContentPlaceHolder1$ddlDistrict"=district[j])
          page2<-submit_form(page1,filled_form1)
          kvk<-html_nodes(page2,css="#ContentPlaceHolder1_ddlKvk > option") %>% html_attr("value")
          kvk<-kvk[-1]
          kvk_name<-html_nodes(page2,css="#ContentPlaceHolder1_ddlKvk > option") %>% html_text()
          kvk_name<-kvk_name[-1]

          #### KVK LOOP ####
          for(k in 1:length(kvk)){
          filled_form2<-set_values(html_form(page2)[[1]],
          "ctl00$ContentPlaceHolder1$ddlState"=states[i],
          "ctl00$ContentPlaceHolder1$ddlDistrict"=district[j],
          "ctl00$ContentPlaceHolder1$ddlKvk"=kvk[k])
          page3<-submit_form(page2,filled_form2)
          contact_text<-gsub("[rn]","",html_nodes(page3,css=".panel-body") %>% html_text())
          if(length(contact_text) == 0){contact_text=""}
          df<-data.frame(cbind(states_name[i],district_name[j],kvk[k],contact_text))
          names(df)<-c("STATE","DISTRICT","KVK","CONTACT_TEXT")
          final_df[i*j*k] = list(df)
          ### WAITTIME TO AVOID HTTP 500 error - So the server is not overloaded
          sleep(5)
          }
          }
          }


          output_df<-data.table::rbindlist(final_df,fill=TRUE)

          # After this perform some string operations to extract the exact information required from the CONTACT_TEXT variable


          The above answer does not use any RSelenium package and I think this is more trustworthy than RSelenium.






          share|improve this answer





















          • Hi @Bharath This works perfectly except that after scraping 94th observation it stops and throws this error: Error in read_xml.response(x$response, ..., as_html = as_html) : Internal Server Error (HTTP 500). In addition: Warning message: In request_POST(session, url = url, body = request$values, encode = request$encode, : Internal Server Error (HTTP 500).
            – Paritosh Sharma
            1 hour ago














          0












          0








          0






          library(rvest)
          url<-"https://kvk.icar.gov.in/facilities_list.aspx"

          page<-html_session(url)
          form<-html_form(page)[[1]]

          states<-html_nodes(page,css="#ContentPlaceHolder1_ddlState > option") %>% html_attr("value")
          states<-states[-1]
          states_name<-html_nodes(page,css="#ContentPlaceHolder1_ddlState > option") %>% html_text()
          states_name<-states_name[-1]

          final_df<-0
          #### STATES LOOP ####
          for(i in 1:length(states)){
          filled_form<-set_values(form,
          "ctl00$ContentPlaceHolder1$ddlState"=states[i])
          page1<-submit_form(page,filled_form)
          district<-html_nodes(page1,css="#ContentPlaceHolder1_ddlDistrict > option") %>% html_attr("value")
          district<-district[-1]
          district_name<-html_nodes(page1,css="#ContentPlaceHolder1_ddlDistrict > option") %>% html_text()
          district_name<-district_name[-1]

          #### DISTRICT LOOP ####
          for(j in 1:length(district)){
          filled_form1<-set_values(html_form(page1)[[1]],
          "ctl00$ContentPlaceHolder1$ddlState"=states[i],
          "ctl00$ContentPlaceHolder1$ddlDistrict"=district[j])
          page2<-submit_form(page1,filled_form1)
          kvk<-html_nodes(page2,css="#ContentPlaceHolder1_ddlKvk > option") %>% html_attr("value")
          kvk<-kvk[-1]
          kvk_name<-html_nodes(page2,css="#ContentPlaceHolder1_ddlKvk > option") %>% html_text()
          kvk_name<-kvk_name[-1]

          #### KVK LOOP ####
          for(k in 1:length(kvk)){
          filled_form2<-set_values(html_form(page2)[[1]],
          "ctl00$ContentPlaceHolder1$ddlState"=states[i],
          "ctl00$ContentPlaceHolder1$ddlDistrict"=district[j],
          "ctl00$ContentPlaceHolder1$ddlKvk"=kvk[k])
          page3<-submit_form(page2,filled_form2)
          contact_text<-gsub("[rn]","",html_nodes(page3,css=".panel-body") %>% html_text())
          if(length(contact_text) == 0){contact_text=""}
          df<-data.frame(cbind(states_name[i],district_name[j],kvk[k],contact_text))
          names(df)<-c("STATE","DISTRICT","KVK","CONTACT_TEXT")
          final_df[i*j*k] = list(df)
          ### WAITTIME TO AVOID HTTP 500 error - So the server is not overloaded
          sleep(5)
          }
          }
          }


          output_df<-data.table::rbindlist(final_df,fill=TRUE)

          # After this perform some string operations to extract the exact information required from the CONTACT_TEXT variable


          The above answer does not use any RSelenium package and I think this is more trustworthy than RSelenium.






          share|improve this answer












          library(rvest)
          url<-"https://kvk.icar.gov.in/facilities_list.aspx"

          page<-html_session(url)
          form<-html_form(page)[[1]]

          states<-html_nodes(page,css="#ContentPlaceHolder1_ddlState > option") %>% html_attr("value")
          states<-states[-1]
          states_name<-html_nodes(page,css="#ContentPlaceHolder1_ddlState > option") %>% html_text()
          states_name<-states_name[-1]

          final_df<-0
          #### STATES LOOP ####
          for(i in 1:length(states)){
          filled_form<-set_values(form,
          "ctl00$ContentPlaceHolder1$ddlState"=states[i])
          page1<-submit_form(page,filled_form)
          district<-html_nodes(page1,css="#ContentPlaceHolder1_ddlDistrict > option") %>% html_attr("value")
          district<-district[-1]
          district_name<-html_nodes(page1,css="#ContentPlaceHolder1_ddlDistrict > option") %>% html_text()
          district_name<-district_name[-1]

          #### DISTRICT LOOP ####
          for(j in 1:length(district)){
          filled_form1<-set_values(html_form(page1)[[1]],
          "ctl00$ContentPlaceHolder1$ddlState"=states[i],
          "ctl00$ContentPlaceHolder1$ddlDistrict"=district[j])
          page2<-submit_form(page1,filled_form1)
          kvk<-html_nodes(page2,css="#ContentPlaceHolder1_ddlKvk > option") %>% html_attr("value")
          kvk<-kvk[-1]
          kvk_name<-html_nodes(page2,css="#ContentPlaceHolder1_ddlKvk > option") %>% html_text()
          kvk_name<-kvk_name[-1]

          #### KVK LOOP ####
          for(k in 1:length(kvk)){
          filled_form2<-set_values(html_form(page2)[[1]],
          "ctl00$ContentPlaceHolder1$ddlState"=states[i],
          "ctl00$ContentPlaceHolder1$ddlDistrict"=district[j],
          "ctl00$ContentPlaceHolder1$ddlKvk"=kvk[k])
          page3<-submit_form(page2,filled_form2)
          contact_text<-gsub("[rn]","",html_nodes(page3,css=".panel-body") %>% html_text())
          if(length(contact_text) == 0){contact_text=""}
          df<-data.frame(cbind(states_name[i],district_name[j],kvk[k],contact_text))
          names(df)<-c("STATE","DISTRICT","KVK","CONTACT_TEXT")
          final_df[i*j*k] = list(df)
          ### WAITTIME TO AVOID HTTP 500 error - So the server is not overloaded
          sleep(5)
          }
          }
          }


          output_df<-data.table::rbindlist(final_df,fill=TRUE)

          # After this perform some string operations to extract the exact information required from the CONTACT_TEXT variable


          The above answer does not use any RSelenium package and I think this is more trustworthy than RSelenium.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          Bharath

          864514




          864514












          • Hi @Bharath This works perfectly except that after scraping 94th observation it stops and throws this error: Error in read_xml.response(x$response, ..., as_html = as_html) : Internal Server Error (HTTP 500). In addition: Warning message: In request_POST(session, url = url, body = request$values, encode = request$encode, : Internal Server Error (HTTP 500).
            – Paritosh Sharma
            1 hour ago


















          • Hi @Bharath This works perfectly except that after scraping 94th observation it stops and throws this error: Error in read_xml.response(x$response, ..., as_html = as_html) : Internal Server Error (HTTP 500). In addition: Warning message: In request_POST(session, url = url, body = request$values, encode = request$encode, : Internal Server Error (HTTP 500).
            – Paritosh Sharma
            1 hour ago
















          Hi @Bharath This works perfectly except that after scraping 94th observation it stops and throws this error: Error in read_xml.response(x$response, ..., as_html = as_html) : Internal Server Error (HTTP 500). In addition: Warning message: In request_POST(session, url = url, body = request$values, encode = request$encode, : Internal Server Error (HTTP 500).
          – Paritosh Sharma
          1 hour ago




          Hi @Bharath This works perfectly except that after scraping 94th observation it stops and throws this error: Error in read_xml.response(x$response, ..., as_html = as_html) : Internal Server Error (HTTP 500). In addition: Warning message: In request_POST(session, url = url, body = request$values, encode = request$encode, : Internal Server Error (HTTP 500).
          – Paritosh Sharma
          1 hour ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53943923%2flooping-in-rselenium-and-scraping%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas