This is the only code I could find to scroll down to the end of the page, nothing else has worked. The problem is that the While True statement never is completed and it continues to try and scroll downward even after it hits bottom and therefore never goes to the next step of printing. How can I end the While True statement and print the results? Thankyou
from selenium import webdriver
url = 'http://www.tradingview.com/screener'
driver = webdriver.Firefox()
driver.get(url)
# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
# Scroll down to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# will give a list of all tickers
tickers = driver.find_elements_by_css_selector('a.tv-screener__symbol')
for index in range(len(tickers)):
print("Row " + tickers[index].text + " ")
Errors I'm receiving
>>> from selenium import webdriver
>>> url = 'http://www.tradingview.com/screener'
>>> driver = webdriver.Firefox()
>>> driver.get(url)
>>>
>>> # Get scroll height
... last_height = driver.execute_script("return document.body.scrollHeight")
>>>
>>> selector = '.js-field-total.tv-screener-table__field-value--total'
>>> matches = driver.find_element_by_css_selector(selector)
>>> matches = int(matches.text.split()[0])
>>>
>>> visible_rows = 0
>>> scrolls = 0
>>>
>>> while visible_rows < matches:
...
File "<stdin>", line 2
^
IndentationError: expected an indented block
>>> driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
File "<stdin>", line 1
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
^
IndentationError: unexpected indent
>>>
>>> # Wait 10 scrolls before updating row information
... if scrolls == 10:
File "<stdin>", line 2
if scrolls == 10:
^
IndentationError: unexpected indent
>>> table = driver.find_elements_by_class_name('tv-data-table__tbody')
File "<stdin>", line 1
table = driver.find_elements_by_class_name('tv-data-table__tbody')
^
IndentationError: unexpected indent
>>> visible_rows = len(table[1].find_elements_by_tag_name('tr'))
File "<stdin>", line 1
visible_rows = len(table[1].find_elements_by_tag_name('tr'))
^
IndentationError: unexpected indent
>>> scrolls = 0
File "<stdin>", line 1
scrolls = 0
^
IndentationError: unexpected indent
>>>
>>> scrolls += 1
File "<stdin>", line 1
scrolls += 1
^
IndentationError: unexpected indent
>>>
>>> # will give a list of all tickers
... tickers = driver.find_elements_by_css_selector('a.tv-screener__symbol')
>>>
>>> for index in range(len(tickers)):
... print("Row " + tickers[index].text + " ")
...