代码之家  ›  专栏  ›  技术社区  ›  koushick

页面对象模型在Python中不起作用

  •  1
  • koushick  · 技术社区  · 7 年前

    我的用于POM的Selenium python代码工作不正常,伙计们,我不知道我做错了什么,我正在粘贴我的代码,请帮我整理一下。

    index.py

    地点是= MyProject/Testmethods/index.py

    from selenium import webdriver
    from selenium.webdriver.support.select import Select
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    import time
    from selenium.common.exceptions import ElementNotVisibleException
    from selenium.common.exceptions import NoSuchElementException
    from selenium.webdriver.common.keys import Keys
    
    
    
    class Wepaythemaxindex(object):
        def Locators(self,driver):
            self.driver = driver
            self.fname = "//input[@id='ContentPlaceHolder1_txt_fname']"
            self.lname = "//input[@id='ContentPlaceHolder1_txt_lname']"
            self.contact = "//input[@id='ContentPlaceHolder1_txt_phone']"
            self.email = "//input[@id='ContentPlaceHolder1_txt_email']"
            self.Location = "//select[@id='ContentPlaceHolder1_drp_pref_loc']"
            self.findus = "//select[@id='ContentPlaceHolder1_ddlSource']"
            self.clicknext = "//a[@id='inner-submit_next']"
            self.year ="//select[@id='ContentPlaceHolder1_Drp_Year']"
            self.make ="//select[@id='ContentPlaceHolder1_drp_Carname']"
            self.textyear ="//input[@id='ContentPlaceHolder1_Drp_Year1']"
            self.textmake = "//input[@id='ContentPlaceHolder1_drp_Carname1']"
            self.getstarted = "//a[@id='ContentPlaceHolder1_vehicle_info_next']"
            self.header ="//span[@class='cd-words-wrapper']"
            self.content = '.paragraphs > .row '
    
    
    
    
        def checkchromedriver(self):
            print("---------Welcome to XXXXXXXXXX-----------")
            print("-------MainForm------------")
            self.UpdateLeadForm(self.driver)
            print("-------SideForm------------")
            self.SideForm(self.driver)
    
    
    
        def UpdateLeadForm(self,driver):
    
         box=["1","2","3","4","5"]
         for button in box:
            # Clicking on the car
            driver.find_element_by_xpath("/html[1]/body[1]/div[1]/div[2]/div[1]/section[1]/div[1]/div[1]/div[2]/div[2]/div[3]/ul[1]/li[" +str(button)+ "]/a[1]/span[1]").click()
    
            # Waiting for 10seconds
            driver.implicitly_wait(10)
    
            self.lead(driver)
    
    
        def SideForm(self,driver):
            form=["1","2","3","4","5"]
            for clickform in form:
                self.Scrolldown(driver)
                driver.find_element_by_xpath("/html[1]/body[1]/div[1]/div[2]/div[1]/section[1]/div[1]/div[1]/div[2]/div[2]/div[3]/ul[2]/li[" +str(clickform)+ "]/a[1]/span[1]").click()
                driver.implicitly_wait(10)
                self.lead(driver)
    
        def Scrolldown(self,driver):
            SCROLL_PAUSE_TIME = 0.5
    
            # Get scroll height
            last_height = driver.execute_script("return document.body.scrollHeight")
    
            while True:
                # Scroll down to bottom
                driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    
                # Wait to load page
                time.sleep(SCROLL_PAUSE_TIME)
    
                # Calculate new scroll height and compare with last scroll height
                new_height = driver.execute_script("return document.body.scrollHeight")
                if new_height == last_height:
                    break
                last_height = new_height
    
        def lead(self,driver):
            # Typing the fname
            elem = driver.find_element_by_xpath(self.fname)
            elem.send_keys("test")
    
            # Typing the Lname
            elem2 = driver.find_element_by_xpath(self.lname)
            elem2.send_keys("test")
    
            # Typing the contact
            driver.find_element_by_xpath(self.contact).send_keys("9176300256")
    
            # Typing the email
            driver.find_element_by_xpath(self.email).send_keys("koushick@rcktechiees.com")
    
            select = Select(driver.find_element_by_xpath(self.Location))
            select.select_by_index(2)
    
            driver.implicitly_wait(10)
    
            driver.get_screenshot_as_file("C:\\Users\\rck\\PycharmProjects\\Wepaythemax\\Screenshot\\Wepaythemax.png")
    
            select1 = Select(driver.find_element_by_xpath(self.findus))
            select1.select_by_visible_text("SIGNAGE")
    
            elem = driver.find_element_by_xpath(self.clicknext)
            elem.click()
    
            year = driver.find_element_by_xpath(self.year)
            try:
             if year.is_displayed():
                selectyear = Select(year)
                selectyear.select_by_index(15)
                print("element is Selected")
             else:
                driver.find_element_by_xpath(self.textyear).send_keys("2016")
            except ElementNotVisibleException:
                print(ElementNotVisibleException)
            except NoSuchElementException:
                print(NoSuchElementException)
    
    
            make = driver.find_element_by_xpath(self.make)
    
            try:
             if make.is_displayed():
                selectmake = Select(make)
                selectmake.select_by_visible_text("HONDA")
                print("Make is Selected")
             else:
                driver.find_element_by_xpath(self.textmake).send_keys("Test")
            except NoSuchElementException:
                print(NoSuchElementException)
            except ElementNotVisibleException:
                print(ElementNotVisibleException)
    
            Getstarted = driver.find_element_by_xpath(self.getstarted)
            Getstarted.click()
    
            driver.implicitly_wait(15)
    
            assert "http://xx.xx.xx.xx:xxxx/Thank_you.aspx" in driver.current_url
            print("Lead Updated Succesfully!")
            driver.get_screenshot_as_file(
                "C:\\Users\\rck\\PycharmProjects\\Wepaythemax\\Screenshot\\Wepaythemaxthankyou.png")
            driver.get("http://xx.xx.xx.xx:xxxx/")
    

    上面我写的代码是针对索引页面的,我需要在最终的测试用例页面中访问它,所以我像下面这样做,

    地点: Myproject/Testcase/FinalTest.py

    import unittest
    from selenium import webdriver
    from TestMethods.index import Wepaythemaxindex
    
    class Wepaythemax(unittest.TestCase):
        def setUp(self):
            self.driverchrome = webdriver.Chrome("F:\\New folder\\chromedriver.exe")
    
    
        def test_Pages(self):
            driver = self.driverchrome
            driver.maximize_window()
            driver.get("http://xx.xx.xx.xx:xxxx/")
            driver.implicitly_wait(10)
            for text_node in driver.find_elements_by_css_selector('.cd-words-wrapper > b'):
                print(text_node.get_attribute('textContent'))
            index = Wepaythemaxindex()
            index.checkchromedriver()
    
    
    
    
    
    
        def tearDown(self):
            self.driverchrome.quit()
    
    if __name__ == '__main__':
        unittest.main()
    
    
    
    The output is : It's Opening the Browser, Works Till text_node, when it comes to accessing the object of index page, it throwing different different error.
    

    1 回复  |  直到 7 年前
        1
  •  2
  •   Luis Estevez    7 年前

    def __init__(self, arg):
        self.someArg = arg