Selenium:如何处理Chrome和Python中的JNLP问题 [英] Selenium: how to handle JNLP issue in Chrome and Python

查看:182
本文介绍了Selenium:如何处理Chrome和Python中的JNLP问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Chrome(Edge或Firefox)JNLP中,警告当我尝试使用Selenium WebDriver打开包含此Java扩展名的网页时,此类型的文件可能会损害您的计算机弹出窗口.有2个按钮-保留以允许继续操作,放弃以...放弃.该警告禁止任何其他操作,因为可能无法允许JNLP并通过Selenium本身从浏览器运行安装.一种可能的解决方案是使用其他浏览器(或IE等淘汰的浏览器)或使用某些解决方法,例如以下一种...

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...

推荐答案

使用Python + Selenium的解决方案
简短说明:在Win32api和Win32con的帮助下,单击保留"按钮并下载文件.其他编程语言或操作系统也可以使用类似的方法或原理.

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)

恭喜-您现在应该站在丑陋的JNLP门卫之后,可以做您需要的任何事情.

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

这篇关于Selenium:如何处理Chrome和Python中的JNLP问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆