本文介绍了使用Python将Selenium Chrome实例发送到后台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python和Selenium打开一个简单的Chrome实例.请在下面找到我的代码:

I am trying to open a simple chrome instance using Python and selenium.Please find my code below:

import time, datetime, sys, os
start_time = time.time()
from datetime import datetime
os.system("cls")
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

CHROME_PATH = 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'
CHROMEDRIVER_PATH = 'C:\\Users\\'+userID+'\\'+filename+'\\chromedriver.exe'
WINDOW_SIZE = "1920,1080"

chrome_options = Options()
chrome_options.add_argument("--window-size=%s" % WINDOW_SIZE)
chrome_options.add_argument("disable-gpu")
chrome_options.add_argument("disable-infobars")
chrome_options.add_argument("--disable-notifications")
chrome_options.binary_location = CHROME_PATH

browser = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH,chrome_options=chrome_options)
time.sleep(1)
browser.get("https://www.google.com")
os.system("cls")
time.sleep(2)

我希望在后台打开它,即当我键入其他内容时,一旦打开此自动chrome实例,鼠标/光标焦点就不应随机进入.

I would like this to open in the background ie when I am typing something else the mouse/cursor focus should not randomly go to this automated chrome instance once it opens.

限制:请注意以下限制:

Limitations: Please note the below limitations:

  1. 我不能使用"--headless"
  2. 我不能使用phantomJS
  3. 我无法使用PyVirtualDisplay,因为最终将为最终用户在Windows机器上部署代码(exe文件).

是否还有其他方法可以将此chrome实例推送到背景?谢谢

Would there be any other way to push this chrome instance to the background?Thanks

推荐答案

Browser Client 中,没有没有编程方式可以打开背景或 背景流程 .

There is no programatic way to open in the Browser Client at the background or as a Background Process.

粗略的替代方法是使用 Headless浏览器,您可以在.

Of coarse the alternative way is to use a Headless Browser and you can find a detailed discussion in Which drivers support "no-browser"/"headless" testing?.

软件测试自动化是一门艺术.您的 Test Framework 应该是:

Software Test Automation is an art. Your Test Framework should be:

此外,

OS级可访问性API的一个优点是,它可以确保输入正确地镜像用户输入,并在必要时允许与主机OS进行交互.但是,从机器利用率的角度来看,这可能会降低性能.

An advantage of an OS-level accessibility API is that it guarantees that inputs correctly mirror user input, and allows interaction with the host OS if necessary. This might, however, have performance penalties from a machine utilisation perspective.

此外,

  • 机器人类别 用于生成本机系统输入事件,用于测试自动化,自运行演示以及需要控制鼠标和键盘的其他应用程序. Robot的主要目的是促进Java平台实现的自动化测试.使用类生成输入事件与将事件发布到AWT事件队列或AWT组件不同,因为事件是在平台的本机输入队列中生成的.例如,Robot.mouseMove实际上将移动鼠标光标,而不仅仅是生成鼠标移动事件.

最后,按照 Internet Explorer和本地事件:

  • 由于InternetExplorerDriver仅适用于Windows,因此它尝试使用所谓的本机"或OS级事件在浏览器中执行鼠标和键盘操作.这与对相同的操作使用模拟的JavaScript事件相反.使用本机事件的优点是它不依赖JavaScript沙箱,并且可以确保JavaScript事件在浏览器中正确传播.但是,当IE浏览器窗口没有焦点并且试图将鼠标悬停在元素上时,鼠标事件当前存在一些问题.

浏览器焦点:

  • 挑战在于,如果窗口没有焦点,则IE本身似乎并不完全尊重我们向IE浏览器窗口发送的Windows消息(WM_MOUSEDOWN和WM_MOUSEUP).具体而言,被单击的元素将在其周围收到一个焦点窗口,但单击不会被该元素处理.可以说,我们根本不应该发送消息.相反,我们应该使用SendInput()API,但是该API明确要求窗口具有焦点. WebDriver项目有两个相互矛盾的目标.

  • The challenge is that IE itself appears to not fully respect the Windows messages we send the IE browser window (WM_MOUSEDOWN and WM_MOUSEUP) if the window doesn't have the focus. Specifically, the element being clicked on will receive a focus window around it, but the click will not be processed by the element. Arguably, we shouldn't be sending messages at all; rather, we should be using the SendInput() API, but that API explicitly requires the window to have the focus. We have two conflicting goals with the WebDriver project.

首先,我们努力尽可能模拟用户.这意味着使用本机事件,而不是使用JavaScript模拟事件.

First, we strive to emulate the user as closely as possible. This means using native events rather than simulating the events using JavaScript.

第二,我们不希望将浏览器窗口的焦点自动化.这意味着仅将浏览器窗口强制为前台是次优的.

Second, we want to not require focus of the browser window being automated. This means that just forcing the browser window to the foreground is suboptimal.

始终将测试环境开发环境分开,并且完全摆脱人工干预.

Always keep the Test Environment seperate from Development Environment and absolutely free from Manual Intervention.

这篇关于使用Python将Selenium Chrome实例发送到后台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 08:00