Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python selenium enter search query then wait

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()

like image 996
Sheshank S. Avatar asked Mar 26 '26 05:03

Sheshank S.


1 Answers

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")))
like image 70
Corey Goldberg Avatar answered Mar 27 '26 20:03

Corey Goldberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!