5

I have this. It's a Python programm to automate the download of documents from our intern company website. I used Webdriver Chrome.

For that, the Python Programm opens a first windows (0), gives the document number and then a second windows will open (1) to click on "print". To be sure that the system will interact with the right windows, I use the switch_to_window().

Issue is that every time the function switch_to_window() is called, it steals the focus and If I'm (for example) typing an email in Outlook, it will steals the focus and my typing will come in the document number and then, the whole Selenium will definitely stops because the Document number can not be found since it's false.

Question: Is there a way to change the windows without stealing the Focus ?

def csv_print():
    global browser
    global h
    numm=0
    numm_1=0
    while numm<h:
        kalknum= browser.find_element_by_id("uc_1")
        searchkalk=browser.find_element_by_id("uc_2")
        kalknum.send_keys(user_entries[numm].get())        
        searchkalk.click()
        fenetre.after(2000)
        
        window_before = browser.window_handles[0]
      
        open_kalk=browser.find_element_by_id("uc_12")
        open_kalk.click()
        fenetre.after(5000)
        
        window_after = browser.window_handles[1]
        browser.switch_to_window(window_after)
        
        element_to_hover_over=browser.find_element_by_id('uc_14')
        hover = ActionChains(browser).move_to_element(element_to_hover_over)
        hover.perform()
        
        element_to_hover_over2=browser.find_element_by_id('uc_15')
        hover2 = ActionChains(browser).move_to_element(element_to_hover_over2)
        hover2.perform()
        
        csv_dwd=browser.find_element_by_id("uc_16")
        csv_dwd.click()
        fenetre.after(5000)
        
        element_to_hover_over3=browser.find_element_by_id('uc_17')
        hover3 = ActionChains(browser).move_to_element(element_to_hover_over3)
        hover3.perform()
        
        kalk_close=browser.find_element_by_id("uc_18")
        kalk_close.click()
        fenetre.after(5000)
        
        browser.close()
        
        browser.switch_to_window(window_before)
        
        kalknum2= browser.find_element_by_id("uc_1")
        kalknum2.clear()
        fenetre.after(1)

            
        numm+=1
        numm_1+=1

Thank you !

Meriole
  • 127
  • 8

0 Answers0