Selenium的Send_Keys无法捕获pyqt5的QLabel文本

时间:2018-10-08 02:49:37

标签: python-3.x selenium user-interface pyqt5

我知道这将是一个简单的修复程序,但我不知道为什么我无法获取Send_Keys来键入pyqt5的QLabel文本

这是我到目前为止所拥有的:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QPushButton, QLabel, QLineEdit, QGridLayout, QGroupBox, QDialog
from PyQt5.QtCore import pyqtSlot
from requests import get
from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from requests import get
import time

class Window(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):

        self.setWindowTitle("Project PiBu!!")

        self.game_name = QLabel("Game Name:", self)
        self.game_line_edit = QLineEdit(self)
        self.search_button = QPushButton("Search", self)
        self.search_button.clicked.connect(self.on_click)
        self.game = QLabel(self)
        self.results = QLabel("Results For:", self)
        self.meta_critic_label = QLabel("Metacritic Score:", self)
        self.user_score_label = QLabel("User Score:", self)
        self.meta_critic_score = ""
        self.user_score_score = ""

        self.createGridLayout()
        self.windowLayout = QVBoxLayout()
        self.windowLayout.addWidget(self.horizontalGroupBox)
        self.setLayout(self.windowLayout)

        self.show()

    def createGridLayout(self):
        self.horizontalGroupBox = QGroupBox()
        self.layout = QGridLayout()
        self.layout.setColumnStretch(1, 3)
        self.layout.setColumnStretch(2, 3)
        self.layout.setColumnStretch(3, 3)
        self.layout.setColumnStretch(4, 3)

        self.layout.addWidget(self.game_name, 0, 0)
        self.layout.addWidget(self.game_line_edit, 0, 1)
        self.layout.addWidget(self.search_button, 0, 2)
        self.layout.addWidget(self.results, 1, 0)
        self.layout.addWidget(self.game, 2, 0)
        self.layout.addWidget(self.meta_critic_label, 1, 1)
        self.layout.addWidget(self.user_score_label, 1, 2)

        self.horizontalGroupBox.setLayout(self.layout)


    @pyqtSlot()
    def on_click(self):
        self.game.setText(self.game_line_edit.text())

        # options = Options()
        # options.add_argument("--headless")  # Runs Chrome in headless mode.
        # options.add_argument('--no-sandbox')  # # Bypass OS security model
        # options.add_argument('start-maximized')
        # options.add_argument('disable-infobars')
        # options.add_argument("--disable-extensions")
        # driver = webdriver.Chrome(chrome_options=options, executable_path='/home/littlejiver/Downloads/chromedriver')

        self.driver = webdriver.Chrome()
        self.driver.get("https://www.metacritic.com/game")

        print("Headless Chrome Initialized on Linux OS")

        self.search_element = self.driver.find_element_by_name("search_term")
        self.search_element.clear()
        self.search_element.send_keys(self.game)

        # self.game_line_edit.setText("")

if __name__ == '__main__':

    app = QApplication(sys.argv)
    win = Window()
    sys.exit(app.exec_())

我收到此错误:

TypeError: object of type 'QLabel' has no len()

我无法弄清楚如何将在“ QLabel”中捕获的输入到“ QLineEdit”中的文本捕获到可以由send_keys“读取”的变量中。

希望这很有道理:)

谢谢!

1 个答案:

答案 0 :(得分:0)

这就像更改一样简单

 self.search_element.send_keys(self.game)

收件人:

self.search_element.send_keys(self.game.text())