How do I Scrape new refreshed data after redirecting to the new page by using selenium











up vote
0
down vote

favorite












I'm Working on a data scraping work by using python and I wanted to do scrape the new redirect page data after clicking on the redirect button.



This is the code which i have tried.



browser =  webdriver.Firefox()
browser.get("https://www.cbsl.gov.lk/en/statistics/economic-indicators")
window_before = browser.window_handles[0]
print(window_before)
browser.find_element_by_xpath('/html/body/div[2]/div[3]/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div[4]/div[2]/p[1]/a').click()
window_after = browser.window_handles[1]
browser.switch_to_window(window_after)
print(window_after)

bs_obj = BSoup(browser.page_source,'lxml')
table = bs_obj.find("table", id="statTB")
print(table)


this will redirect to the new page. but after print the table it was not showing anything. I think still it was trying on the old page.










share|improve this question


























    up vote
    0
    down vote

    favorite












    I'm Working on a data scraping work by using python and I wanted to do scrape the new redirect page data after clicking on the redirect button.



    This is the code which i have tried.



    browser =  webdriver.Firefox()
    browser.get("https://www.cbsl.gov.lk/en/statistics/economic-indicators")
    window_before = browser.window_handles[0]
    print(window_before)
    browser.find_element_by_xpath('/html/body/div[2]/div[3]/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div[4]/div[2]/p[1]/a').click()
    window_after = browser.window_handles[1]
    browser.switch_to_window(window_after)
    print(window_after)

    bs_obj = BSoup(browser.page_source,'lxml')
    table = bs_obj.find("table", id="statTB")
    print(table)


    this will redirect to the new page. but after print the table it was not showing anything. I think still it was trying on the old page.










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm Working on a data scraping work by using python and I wanted to do scrape the new redirect page data after clicking on the redirect button.



      This is the code which i have tried.



      browser =  webdriver.Firefox()
      browser.get("https://www.cbsl.gov.lk/en/statistics/economic-indicators")
      window_before = browser.window_handles[0]
      print(window_before)
      browser.find_element_by_xpath('/html/body/div[2]/div[3]/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div[4]/div[2]/p[1]/a').click()
      window_after = browser.window_handles[1]
      browser.switch_to_window(window_after)
      print(window_after)

      bs_obj = BSoup(browser.page_source,'lxml')
      table = bs_obj.find("table", id="statTB")
      print(table)


      this will redirect to the new page. but after print the table it was not showing anything. I think still it was trying on the old page.










      share|improve this question













      I'm Working on a data scraping work by using python and I wanted to do scrape the new redirect page data after clicking on the redirect button.



      This is the code which i have tried.



      browser =  webdriver.Firefox()
      browser.get("https://www.cbsl.gov.lk/en/statistics/economic-indicators")
      window_before = browser.window_handles[0]
      print(window_before)
      browser.find_element_by_xpath('/html/body/div[2]/div[3]/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div[4]/div[2]/p[1]/a').click()
      window_after = browser.window_handles[1]
      browser.switch_to_window(window_after)
      print(window_after)

      bs_obj = BSoup(browser.page_source,'lxml')
      table = bs_obj.find("table", id="statTB")
      print(table)


      this will redirect to the new page. but after print the table it was not showing anything. I think still it was trying on the old page.







      python selenium selenium-webdriver web-scraping webdriver






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 7 at 10:14









      Haz

      345




      345
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          you need multiple WebDriverWait, waiting second window and page loaded



          from selenium.webdriver.common.by import By
          from selenium.webdriver.support.ui import WebDriverWait
          from selenium.webdriver.support import expected_conditions as EC

          browser.get("https://www.cbsl.gov.lk/en/statistics/economic-indicators")
          window_before = browser.window_handles[0]
          print(window_before)
          browser.find_element_by_xpath('/html/body/div[2]/div[3]/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div[4]/div[2]/p[1]/a').click()
          WebDriverWait(browser, 20).until(EC.number_of_windows_to_be(2))
          window_after = browser.window_handles[1]
          browser.switch_to_window(window_after)
          print(window_after)

          myElem = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'Grid')))
          bs_obj = BeautifulSoup(browser.page_source, 'lxml')

          table = bs_obj.find("table", id="statTB")
          print(table)





          share|improve this answer





















          • Actually, OP don't need to wait for second window (at least this is not a problem), because in case second window is not opened OP should get IndexError on window_after = browser.window_handles[1] line
            – Andersson
            Nov 7 at 11:18


















          up vote
          1
          down vote













          No. When you switched to new window, browser.page_source returns you HTML of new
          window, but you might need to wait until required table appeared in DOM:



          from selenium.webdriver.common.by import By
          from selenium.webdriver.support.ui import WebDriverWait
          from selenium.webdriver.support import expected_conditions as EC

          ...
          browser.switch_to_window(window_after)
          table = WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.ID, "statTB")))
          print(table.text)





          share|improve this answer





















            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',
            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%2f53187391%2fhow-do-i-scrape-new-refreshed-data-after-redirecting-to-the-new-page-by-using-se%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            you need multiple WebDriverWait, waiting second window and page loaded



            from selenium.webdriver.common.by import By
            from selenium.webdriver.support.ui import WebDriverWait
            from selenium.webdriver.support import expected_conditions as EC

            browser.get("https://www.cbsl.gov.lk/en/statistics/economic-indicators")
            window_before = browser.window_handles[0]
            print(window_before)
            browser.find_element_by_xpath('/html/body/div[2]/div[3]/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div[4]/div[2]/p[1]/a').click()
            WebDriverWait(browser, 20).until(EC.number_of_windows_to_be(2))
            window_after = browser.window_handles[1]
            browser.switch_to_window(window_after)
            print(window_after)

            myElem = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'Grid')))
            bs_obj = BeautifulSoup(browser.page_source, 'lxml')

            table = bs_obj.find("table", id="statTB")
            print(table)





            share|improve this answer





















            • Actually, OP don't need to wait for second window (at least this is not a problem), because in case second window is not opened OP should get IndexError on window_after = browser.window_handles[1] line
              – Andersson
              Nov 7 at 11:18















            up vote
            1
            down vote



            accepted










            you need multiple WebDriverWait, waiting second window and page loaded



            from selenium.webdriver.common.by import By
            from selenium.webdriver.support.ui import WebDriverWait
            from selenium.webdriver.support import expected_conditions as EC

            browser.get("https://www.cbsl.gov.lk/en/statistics/economic-indicators")
            window_before = browser.window_handles[0]
            print(window_before)
            browser.find_element_by_xpath('/html/body/div[2]/div[3]/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div[4]/div[2]/p[1]/a').click()
            WebDriverWait(browser, 20).until(EC.number_of_windows_to_be(2))
            window_after = browser.window_handles[1]
            browser.switch_to_window(window_after)
            print(window_after)

            myElem = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'Grid')))
            bs_obj = BeautifulSoup(browser.page_source, 'lxml')

            table = bs_obj.find("table", id="statTB")
            print(table)





            share|improve this answer





















            • Actually, OP don't need to wait for second window (at least this is not a problem), because in case second window is not opened OP should get IndexError on window_after = browser.window_handles[1] line
              – Andersson
              Nov 7 at 11:18













            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            you need multiple WebDriverWait, waiting second window and page loaded



            from selenium.webdriver.common.by import By
            from selenium.webdriver.support.ui import WebDriverWait
            from selenium.webdriver.support import expected_conditions as EC

            browser.get("https://www.cbsl.gov.lk/en/statistics/economic-indicators")
            window_before = browser.window_handles[0]
            print(window_before)
            browser.find_element_by_xpath('/html/body/div[2]/div[3]/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div[4]/div[2]/p[1]/a').click()
            WebDriverWait(browser, 20).until(EC.number_of_windows_to_be(2))
            window_after = browser.window_handles[1]
            browser.switch_to_window(window_after)
            print(window_after)

            myElem = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'Grid')))
            bs_obj = BeautifulSoup(browser.page_source, 'lxml')

            table = bs_obj.find("table", id="statTB")
            print(table)





            share|improve this answer












            you need multiple WebDriverWait, waiting second window and page loaded



            from selenium.webdriver.common.by import By
            from selenium.webdriver.support.ui import WebDriverWait
            from selenium.webdriver.support import expected_conditions as EC

            browser.get("https://www.cbsl.gov.lk/en/statistics/economic-indicators")
            window_before = browser.window_handles[0]
            print(window_before)
            browser.find_element_by_xpath('/html/body/div[2]/div[3]/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div[4]/div[2]/p[1]/a').click()
            WebDriverWait(browser, 20).until(EC.number_of_windows_to_be(2))
            window_after = browser.window_handles[1]
            browser.switch_to_window(window_after)
            print(window_after)

            myElem = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'Grid')))
            bs_obj = BeautifulSoup(browser.page_source, 'lxml')

            table = bs_obj.find("table", id="statTB")
            print(table)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 7 at 11:10









            ewwink

            5,95422232




            5,95422232












            • Actually, OP don't need to wait for second window (at least this is not a problem), because in case second window is not opened OP should get IndexError on window_after = browser.window_handles[1] line
              – Andersson
              Nov 7 at 11:18


















            • Actually, OP don't need to wait for second window (at least this is not a problem), because in case second window is not opened OP should get IndexError on window_after = browser.window_handles[1] line
              – Andersson
              Nov 7 at 11:18
















            Actually, OP don't need to wait for second window (at least this is not a problem), because in case second window is not opened OP should get IndexError on window_after = browser.window_handles[1] line
            – Andersson
            Nov 7 at 11:18




            Actually, OP don't need to wait for second window (at least this is not a problem), because in case second window is not opened OP should get IndexError on window_after = browser.window_handles[1] line
            – Andersson
            Nov 7 at 11:18












            up vote
            1
            down vote













            No. When you switched to new window, browser.page_source returns you HTML of new
            window, but you might need to wait until required table appeared in DOM:



            from selenium.webdriver.common.by import By
            from selenium.webdriver.support.ui import WebDriverWait
            from selenium.webdriver.support import expected_conditions as EC

            ...
            browser.switch_to_window(window_after)
            table = WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.ID, "statTB")))
            print(table.text)





            share|improve this answer

























              up vote
              1
              down vote













              No. When you switched to new window, browser.page_source returns you HTML of new
              window, but you might need to wait until required table appeared in DOM:



              from selenium.webdriver.common.by import By
              from selenium.webdriver.support.ui import WebDriverWait
              from selenium.webdriver.support import expected_conditions as EC

              ...
              browser.switch_to_window(window_after)
              table = WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.ID, "statTB")))
              print(table.text)





              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                No. When you switched to new window, browser.page_source returns you HTML of new
                window, but you might need to wait until required table appeared in DOM:



                from selenium.webdriver.common.by import By
                from selenium.webdriver.support.ui import WebDriverWait
                from selenium.webdriver.support import expected_conditions as EC

                ...
                browser.switch_to_window(window_after)
                table = WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.ID, "statTB")))
                print(table.text)





                share|improve this answer












                No. When you switched to new window, browser.page_source returns you HTML of new
                window, but you might need to wait until required table appeared in DOM:



                from selenium.webdriver.common.by import By
                from selenium.webdriver.support.ui import WebDriverWait
                from selenium.webdriver.support import expected_conditions as EC

                ...
                browser.switch_to_window(window_after)
                table = WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.ID, "statTB")))
                print(table.text)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 7 at 11:01









                Andersson

                34.7k103065




                34.7k103065






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53187391%2fhow-do-i-scrape-new-refreshed-data-after-redirecting-to-the-new-page-by-using-se%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







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini