actions = ActionChains(driver)
actions.send_keys(search_query + Keys.ENTER)
actions.perform()
# code to wait until page loads
src = driver.page_source
How would I implement this? I'm trying to send keys to the search box, which i have, and then after the .perform I would like it to wait till the search results load, then get the source. Is that possible with selenium?
Trying to find a way better than time.sleep(2) Maybe something like driver.wait_until_page_loads()
I would like it to wait till the search results load
What you are looking for is a WebDriverWait (aka an "Explicit Wait").
You want to choose a specific element that signifies the results are loaded and wait for that. Suppose you are waiting for the presence of an element that can be located with the id of mySearchResults. The code would look like this.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# create driver and execute search here
# now wait for the presence of the `mySearchResults` element.
# if it is not found within 10 secs, a `TimeOutException` is raised.
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "mySearchResults")))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With