1

I am automating an application that downloads files using selenium and python. I am getting timeout exception even though I have handled it in the python script. The timeout exception handles timeout most of the time but eventually my code crashes. Below is my script please suggest how I can avoid the timeout exception in order to make the script more robust. The 1st part of the code is to launch the application and reach until the point where it clicks the file to be downloaded. Each page has 20 files to be downloaded and candidate function does it for each page.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

chromeOptions = webdriver.ChromeOptions()
prefs = {"download.default_directory" : "Location"}
chromeOptions.add_experimental_option("prefs",prefs)
driver=webdriver.Chrome("\\chromedriver.exe",chrome_options=chromeOptions)
driver.implicitly_wait(30)
driver.maximize_window()
driver.implicitly_wait(100)
driver.get("URL") 
username=driver.find_element_by_id("user")
username.clear()
username.send_keys("mmnba")
password=driver.find_element_by_id("pw")
password.clear()
password.send_keys("dfsdf")
driver.implicitly_wait(20)
login=driver.find_element_by_id("loginButton")
login.click()
driver.implicitly_wait(20)
rollup=driver.find_element_by_xpath("")
rollup.click()
driver.switch_to.frame(0)
link=driver.find_element_by_xpath("//td[@id="""")
link.click()
driver.implicitly_wait(20)
pageList=driver.find_elements_by_xpath("")

def candidate():
    global k,page_num,driver
    for j in range(0,20):
        try: 
            driver.implicitly_wait(50)   
            employeeList=driver.find_elements_by_xpath("")
            employeeList[j].click()
            driver.switch_to_default_content()
            driver.implicitly_wait(50)
            driver.switch_to.frame("detail")
            wait = WebDriverWait(driver,20)
            resume = 
 wait.until(EC.presence_of_element_located((By.XPATH,"")))
            driver.implicitly_wait(50)
            resume.click()
            download = 
 wait.until(EC.presence_of_element_located((By.XPATH,'//a[@title="Download 
 Resume"]')))

            driver.implicitly_wait(50)
            download.click()
            driver.implicitly_wait(50)
            driver.switch_to.frame("RTFVIEWER_MS")
            msword = 
wait.until(EC.presence_of_element_located((By.XPATH,"")))
            driver.implicitly_wait(50)            
            msword.click()
        except TimeoutException as ex1:
            print("Exception has been thrown"+str(ex1))
            print(j)
            driver.switch_to_default_content()
            driver.switch_to.frame(0)
            continue

      # Code for moving from one page to the next
k=0
page_num=0    
candidate()
pageList=driver.find_elements_by_xpath("")

for i in range(0,10):
    driver.implicitly_wait(50)    

pageList=driver.find_elements_by_xpath("")
    if i !=9:
        page_num=i
print("page number is "+str(page_num))
pageList[i].click()    
candidate()
if i==9:
    k=1
    pageList=driver.find_elements_by_xpath("")
    i=1
    page_num=i
    while i<12:    
        driver.implicitly_wait(50)
        pageList=driver.find_elements_by_xpath("")
        if i ==10:
            pageList[i].click()
            candidate()
            k=k+1
            print("k is "+str(k))
            i=1
            page_num=i
        else:   
            page_num=i
            print("page number is "+str(page_num))
            pageList[i].click()
            candidate()
            i=i+1

#EDIT : Adding Traceback

Traceback (most recent call last):

File "<ipython-input-7-f663f73d040e>", line 1, in <module>
runfile('D:/automate_v6.py', wdir='D:/')

File "C:\Users\Vishnu\Anaconda2\lib\site-
packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in 
runfile
execfile(filename, namespace)

File "C:\Users\Vishnu\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)

 File "D:/automate_v6.py", line 214, in <module>
 candidate()

File "D:/automate_v6.py", line 177, in candidate
driver.switch_to_default_content()

File "C:\Users\Vishnu\Anaconda2\lib\site-packages\selenium-2.53.1-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 575, in 
switch_to_default_content
self._switch_to.default_content()

File "C:\Users\Vishnu\Anaconda2\lib\site-packages\selenium-2.53.1-py2.7.egg\selenium\webdriver\remote\switch_to.py", line 52, in default_content
self._driver.execute(Command.SWITCH_TO_FRAME, {'id': None})

File "C:\Users\Vishnu\Anaconda2\lib\site-packages\selenium-2.53.1-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 233, in execute
self.error_handler.check_response(response)

File "C:\Users\Vishnu\Anaconda2\lib\site-packages\selenium-2.53.1-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)

TimeoutException: timeout
(Session info: chrome=60.0.3112.101)
(Driver info: chromedriver=2.31.488763 
(092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 10.0.14393 
x86_64)
Vishnu
  • 110
  • 2
  • 10

0 Answers0