PythonLearner PythonLearner - 1 year ago 171
Python Question

How to tell Python to wait for Selenium?

I am able to filter the data with the code below but the Export to Excel does not work. I request your kind assistance on how to improve my snippet below to instruct Python to wait for the data to get fully loaded and then download the excel file to the desired folder.

from selenium import webdriver
from import WebDriverWait
from import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

driver = webdriver.Chrome("C:\Python27\Scripts\chromedriver.exe")

number_option = driver.find_element_by_id("cplMain_btnSearch")

delay = 3
WebDriverWait(driver, delay).until(EC.presence_of_element_located(driver.find_element_by_id("cplMain_btnSearch")))
print "Page is ready!"
except TimeoutException:
print "Loading took too much time!"

search_button = driver.find_element_by_id("cplMain_btnExportToExcel")

options.add_argument("download.default_directory=C:\Users\Patrick\Desktop\Programming\R Files")
driver = webdriver.Chrome(chrome_options=options)


The Error:

Traceback (most recent call last):
File "C:\Users\Patrick\Desktop\Programming\", line 14, in <module>
WebDriverWait(driver, delay).until(EC.presence_of_element_located(driver.find_element_by_id("cplMain_btnSearch")))

File "C:\Python27\lib\site-packages\selenium\webdriver\support\", line 71, in until
value = method(self._driver)
File "C:\Python27\lib\site-packages\selenium\webdriver\support\", line 63, in __call__
return _find_element(driver, self.locator)
File "C:\Python27\lib\site-packages\selenium\webdriver\support\", line 328, in _find_element
return driver.find_element(*by)
TypeError: find_element() argument after * must be a sequence, not WebElement

Answer Source

Try to replace this

 WebDriverWait(driver, delay).until(EC.presence_of_element_located(driver.find_element_by_id("cplMain_btnSearch"))

with this

from import By

WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, "cplMain_btnSearch")))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download