试试看:from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication, QStatusBar
class AutoTestWindow(QMainWindow):
def __init__(self):
super().__init__()
self.driver = None # +++
self.query = "Тензор" # +++
self.initUI()
def initUI(self):
btn1 = QPushButton("Test № 1 ", self)
btn1.move(30, 50)
btn2 = QPushButton("Test № 2", self)
btn2.move(150, 50)
#btn1.clicked.connect(self.buttonClicked) # -
#btn2.clicked.connect(self.buttonClicked) # -
btn1.clicked.connect(self.init_driver) # +++
btn2.clicked.connect(lambda checked : self.findelem(self.driver, self.query)) # +++
self.statusBar().showMessage('Запуск тестов', 5000)
self.setGeometry(300, 300, 290, 150)
self.setWindowTitle('Запуск тестов')
self.show()
# -
#def buttonClicked(self):
# sender = self.sender()
# self.statusBar().showMessage(sender.text() + ' test started')
# +++
def init_driver(self):
self.statusBar().showMessage('Test № 1 test started')
#driver = webdriver.Chrome("/Users/alexeynikitin/Desktop/chromedriver")
self.driver = webdriver.Chrome()
self.driver.wait = WebDriverWait(self.driver, 10)
#return self.driver
# +++
def findelem(self, driver, query):
print("\ndriver=`{}`, \nquery=`{}`\n".format(driver, query))
self.statusBar().showMessage('Test № 2 test started')
if driver is not None:
driver.get("https://www.yandex.ru/")
try:
box = driver.wait.until(ec.presence_of_element_located(
(By.ID, "text")))
button = driver.wait.until(ec.visibility_of_element_located(
(By.CLASS_NAME, "search2__button")))
box.send_keys(query)
suggestion_box = driver.wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, "body > div.i-bem.popup.suggest2.suggest2_theme_flat.suggest2_size_m.suggest2_adaptive_yes.suggest2_type_advanced.suggest2_ahead_yes.popup_adaptive_yes.popup_animate_no.popup_autoclosable_yes.popup_theme_ffffff.suggest2-detect_js_inited.suggest2_js_inited.popup_js_inited.popup_to_bottom.popup_visibility_visible")))
try:
button.click()
except ElementNotVisibleException:
button = driver.wait.until(ec.visibility_of_element_located(
(By.CLASS_NAME, "search2__button")))
except TimeoutException:
print("ничего не нашел на https://www.yandex.ru/")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = AutoTestWindow()
sys.exit(app.exec_())