from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
chrome_options = webdriver.ChromeOptions()
s=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s,options=chrome_options,service_log_path='NUL')
#driver.get("http://www.stackoverflow.com")
original_window = driver.current_window_handle
print(original_window)
以下代码的输出是:
1.立即打开谷歌chrom的网址:www.stackoverflow.com
1.在控制台中打印以下内容:
====== WebDriver manager ======
Current google-chrome version is 96.0.4664
Get LATEST chromedriver version for 96.0.4664 google-chrome
Driver [C:/Users/Χρήστος Παππάς/.wdm/drivers/chromedriver/win32/96.0.4664.45/chromedriver.exe] found in cache
DevTools listening on ws://127.0.0.1:64846/devtools/browser/fe12038b-ecd2-4c70-8cf3-ab0a8aeadbc3
CDwindow-1B93BAC67BF5856846B6207B11EF4F1E
其中CDwindow-1B93BAC67BF5856846B6207B11EF4F1E
是driver.current_window_handle
值。
所以我有问题确定 selenium 浏览器的窗口ID。
如果这个值是一个int数字,那么我可以在QWindow qt 5类的fromWinId
方法中使用它作为输入。
那么,如何在pyqt 5应用程序中导入selenium浏览器而不是QWebEngine
和QWebKit
?
请回答基于Arch-Linux的平台支持(因此不接受基于win32 gui的答案)。
**编辑:**可能的解决方案:
我意识到pywin 32库可以在msys 2平台上使用。所以我做了这样的东西:
from PyQt5 import QtCore, QtGui, QtWidgets
import win32gui
import win32con
import winxpgui
import win32api
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import time
import ctypes
user32 = ctypes.windll.user32
width,height = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
#self.verticalLayout.addWidget(self.frame)
MainWindow.setCentralWidget(self.centralwidget)
self.chrome_options = Options()
self.chrome_options.add_argument("disable-infobars")
#self.chrome_options.add_argument("--window-size=0,0")
self.chrome_options.add_argument("--kiosk")
self.chrome_options.add_argument("--window-size="+str(int(2*width))+","+str(int(2*height)))
self.chrome_options.add_argument("--window-position=-10,-10")
self.chrome_options.add_argument("--app=http://www.in.gr/");
self.s=Service(ChromeDriverManager().install())
self.driver = webdriver.Chrome(service=self.s,options=self.chrome_options,service_log_path='NUL')
self.driver.get("http://www.in.gr")
time.sleep(0.5)
self.hwnd = 0
self.tries = 30
self.total_tries = 0
while(self.hwnd==0 and self.total_tries<=self.tries):
try:
win32gui.EnumWindows(self.hwnd_method, None)
#win32gui.SetWindowLong (self.hwnd, win32con.GWL_EXSTYLE, win32gui.GetWindowLong (self.hwnd, win32con.GWL_EXSTYLE ) | win32con.WS_EX_LAYERED )
#winxpgui.SetLayeredWindowAttributes(self.hwnd, win32api.RGB(0,0,0), 255, win32con.LWA_ALPHA)
self.embed_window = QtGui.QWindow.fromWinId(self.hwnd)
self.embed_widget = QtWidgets.QWidget.createWindowContainer(self.embed_window)
self.verticalLayout.addWidget(self.embed_widget)
#self.driver.execute_script("document.documentElement.requestFullscreen();")
self.tries+= 1
break
time.sleep(1)
except Exception as e:
print(e)
self.tries += 1
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def hwnd_method(self, hwnd, ctx):
window_title = win32gui.GetWindowText(hwnd)
if "in.gr" in window_title.lower():
self.hwnd = hwnd
'''
old_style = win32gui.GetWindowLong(hwnd, -16)
# building the new style(old style AND NOT Maximize AND NOT Minimize)
new_style = old_style & ~win32con.WS_MAXIMIZEBOX & ~win32con.WS_MINIMIZEBOX
# setting new style
win32gui.SetWindowLong(hwnd, -16, new_style)
# updating non - client area
win32gui.SetWindowPos(hwnd, 0, 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_NOZORDER | win32con.SWP_FRAMECHANGED)
win32gui.UpdateWindow(hwnd)
'''
#win32gui.ShowWindow(hwnd , win32con.SW_HIDE)
#win32gui.SetWindowLong (hwnd, win32con.GWL_EXSTYLE, win32gui.GetWindowLong (hwnd, win32con.GWL_EXSTYLE ) | win32con.WS_EX_LAYERED )
#winxpgui.SetLayeredWindowAttributes(hwnd, win32api.RGB(0,0,0), 0, win32con.LWA_ALPHA)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
输出:
up代码的唯一问题是:我不希望全屏网站是可见的,直到该网站嵌入到QMainWindow。有什么可能的解决办法吗?
2条答案
按热度按时间laik7k3q1#
您可以尝试添加以下内容:
我有个问题要问你希望你能了解一些
我有一个程序可以同时嵌入多个chrome窗口。但错误开始发生时,我从第二个浏览器嵌入。
我可以单击按钮,但无法在以前打开的嵌入式窗口中的任何位置键入内容。当我调整嵌入窗口大小时也会发生同样的情况
谢谢你的阅读,我很高兴我得到了你的一些帮助
vxqlmq5t2#
您必须删除self.chrome_options.add_argument(“--kiosk”)。它启用了kiosk模式。