I have a script with python and selenium to scrape google results.. It works, but I'm looking for a better solution to wait until all 100
search results are fetched
I use this solution to wait until the search is done
driver.wait.until(EC.presence_of_element_located(
(By.ID, 'resultStats')))
This works, but I need to get 100
search results so I do this
driver.get(driver.current_url+'&num=100')
But now its not possible to re-use this line because the element ID is already written to the page..
driver.wait.until(EC.presence_of_element_located(
(By.ID, 'resultStats')))
Instead I use this solution, but its not a consistent solution (if the request takes more than 5 secs)
time.sleep(5)
code
url = 'https://www.google.com'
driver.get(url)
try:
box = driver.wait.until(EC.presence_of_element_located(
(By.NAME, 'q')))
box.send_keys(query.decode('utf-8'))
button = driver.wait.until(EC.element_to_be_clickable(
(By.NAME, 'btnG')))
button.click()
except TimeoutException:
error('Box or Button not found in google.com')
try:
driver.wait.until(EC.presence_of_element_located(
(By.ID, 'resultStats')))
driver.get(driver.current_url+'&num=100')
# Need a better solution to wait until all results are loaded
time.sleep(5)
print driver.find_element_by_tag_name('body').get_attribute('innerHTML').encode('utf-8')
except TimeoutException:
error('No results returned by Google. Could be HTTP 503 response')