Selenium: how to handle JNLP issue in Chrome and Python

两盒软妹~` 提交于 2021-02-05 09:29:17

问题


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 WebDriver. There are 2 buttons - Keep to allow proceeding and Discard to...discard. The warning forbid any other action because it's probably not possible to allow JNLP and run its installation from browser via Selenium itself. One possible solution is to use different browser (or retired browser like IE) or to use some workaround, like the one bellow...


回答1:


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.



来源:https://stackoverflow.com/questions/65798335/selenium-how-to-handle-jnlp-issue-in-chrome-and-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!