PythonLearner PythonLearner - 12 days ago 5
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 selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

driver = webdriver.Chrome("C:\Python27\Scripts\chromedriver.exe")
driver.get("https://etrakit.friscotexas.gov/Search/permit.aspx")

number_option = driver.find_element_by_id("cplMain_btnSearch")
number_option.click()

delay = 3
try:
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")
search_button.click()

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

driver.close()


The Error:

Traceback (most recent call last):
File "C:\Users\Patrick\Desktop\Programming\aspxscraping.py", 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\wait.py", line 71, in until
value = method(self._driver)
File "C:\Python27\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 63, in __call__
return _find_element(driver, self.locator)
File "C:\Python27\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 328, in _find_element
return driver.find_element(*by)
TypeError: find_element() argument after * must be a sequence, not WebElement

Answer

Try to replace this

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

with this

from selenium.webdriver.common.by import By

WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, "cplMain_btnSearch")))
Comments