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.
python selenium selenium-webdriver web-scraping webdriver
add a comment |
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.
python selenium selenium-webdriver web-scraping webdriver
add a comment |
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.
python selenium selenium-webdriver web-scraping webdriver
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
python selenium selenium-webdriver web-scraping webdriver
asked Nov 7 at 10:14
Haz
345
345
add a comment |
add a comment |
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)
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 getIndexError
onwindow_after = browser.window_handles[1]
line
– Andersson
Nov 7 at 11:18
add a comment |
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)
add a comment |
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)
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 getIndexError
onwindow_after = browser.window_handles[1]
line
– Andersson
Nov 7 at 11:18
add a comment |
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)
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 getIndexError
onwindow_after = browser.window_handles[1]
line
– Andersson
Nov 7 at 11:18
add a comment |
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)
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)
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 getIndexError
onwindow_after = browser.window_handles[1]
line
– Andersson
Nov 7 at 11:18
add a comment |
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 getIndexError
onwindow_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
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered Nov 7 at 11:01
Andersson
34.7k103065
34.7k103065
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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