将Selenium输入QLineEdit后尝试捕获文本

时间:2018-10-08 05:06:19

标签: python python-3.x selenium pyqt pyqt5

在输入QLineEdit的字符串后,尝试从Metacritic的搜索栏中获取硒以抢占第一个游戏的名称

除了获取游戏名称之外,我已经进行了所有工作

我认为我的元素有误,并且在html方面遇到了问题

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 = QLabel("", self)
        self.user_score_score = QLabel("", self)
        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.layout.addWidget(self.meta_critic_score, 2, 1)

        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")


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

        self.game.setText(self.driver.find_elements_by_class_name("title"))

        self.game_line_edit.setText("")

if __name__ == '__main__':

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

我认为这是问题所在:

self.game.setText(self.driver.find_elements_by_class_name("title"))

但是我不知道我在寻找什么元素

0 个答案:

没有答案