Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
6.2k views
in Technique[技术] by (71.8m points)

I have been trying to Run a simple Page Object Model in Python with Selenium but for some reason the browser do not open

''' from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys import pytest

#Google.py File class Google: txtbox_searchbar_xpath = "//input[@class='gLFyf gsfi']"

def __init__(self,driver):
    self.driver = driver

def enterSearchTerm(self,search_term):
    self.driver.find_element(By.XPATH, self.txtbox_searchbar_xpath).claer()
    self.driver.find_element(By.XPATH, self.txtbox_searchbar_xpath).send_keys(search_term,Keys.RETURN)

'''

''' from selenium import webdriver from PageObjects.Google import Google import pytest

#test_Google.py class Google_Test:

url = "https://www.google.com/"
search_word = "Python.org"
searchbar_xpath ="//input[@class='gLFyf gsfi']"

def test_searchGoogle(self):
    self.driver = webdriver.Chrome(executable_path="C:\Users\mm195\PycharmProjects\pythonProject\drivers\chromedriver.exe")
    self.driver.get(self.url)
    # Initiate Google Class
    self.google = Google(self.driver)
    self.google.enterSearchTerm(self.search_word)
    act_title = self.driver.title
    if act_title == "Google":
        assert True
    else:
        assert False

'''

question from:https://stackoverflow.com/questions/66066014/i-have-been-trying-to-run-a-simple-page-object-model-in-python-with-selenium-but

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This answer solves only title-question "why the browser is not opening". Pytest naming convention requires to have Classes and method starting with "Test/test".

So you need to change Class name to Test_Google.


Update - 06/02/2021 - 19:26 PLN Time

How are you running tests? Pytest has own test discovery mechanism. If you call simply from cmd line:

pytest test_script.py

then it should discover existing test/tests



Update - 08/02/2021 - 12:54 PLN Time

So

You would need to provide more information:

  • directory structure
  • errors during firs/second run

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.7k users

...