Selenium: how to handle JNLP issue in Chrome and Python

前端 未结 1 1896
一个人的身影
一个人的身影 2021-01-28 14:42

In Chrome (Edge or Firefox) JNLP warning This type of file can harm your computer popups when I try to open web page containing this Java extension with Selenium WebDri

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-28 15:40

    Solution with use of Python + Selenium
    Short description: click on Keep button and downloaded file with help of win32api and win32con. Similar approach or principle can be used in other programing languages or operation systems.

    from selenium import webdriver
    import time
    import win32api, win32con
    
    # define click action - click with left mouse button at a position
    def click_at(coord):
        # move mouse cursor on requested position (coord is tuple with X and Y position)
        win32api.SetCursorPos(coord)
        # press and release LMB
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, coord[0], coord[1])
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, coord[0], coord[1])
    
    # set path to chromedriver and start as usually
    chromeserver = r'...\chromedriver.exe'
    driver = webdriver.Chrome(executable_path=chromeserver)
    # run page containing JNLP
    driver.get('http://somepagewithjnpl.com')
    
    # maximize browser window 
    driver.maximize_window()
    # get size of the window via Selenium - returns dictionary {'width': int, 'height': int}
    # (note: 0;0 point is located to upper left corner)
    current_size = driver.get_window_size()
    # set coordinations where mouse should click on Keep button 
    # (note: position could be slightly different in different browsers or languages - successfully 
    # tested in Chrome + Win10 + Czech and English langs) 
    coord = (current_size['width'] // 4, current_size['height']-50)
    # click on Keep button to start downloading JNLP file
    click_at(coord)
    # take a short breather to give browser time to download JNLP file, notification changes then
    time.sleep(5)
    
    # set new coords and click at JNLP file to install and run Java WebStart
    coord = (50, current_size['height']-50)
    click_at(coord)
    # center mouse cursor (not necessary but I like it)
    coord = (current_size['width'] // 2, current_size['height'] // 2)
    win32api.SetCursorPos(coord)
    # take a breather again to give browser time for JNLP installation and Java activation
    time.sleep(10)
    

    Congratulations - you should now stand behind ugly JNLP gate keeper and may do anything you need.

    0 讨论(0)
提交回复
热议问题