python - 页面对象模型不工作 Selenium Python

标签 python selenium selenium-webdriver pageobjects

我的 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("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a2c9cdd7d1cacbc1c9e2d0c1c9d6c7c1cacbc7c7d18cc1cdcf" rel="noreferrer noopener nofollow">[email protected]</a>")

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

我编写的上述代码是针对索引页面的,我需要在最终测试用例页面中访问它,所以我做了如下操作,

最终测试.py

位置: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.

在 Java 中,我已经解决了我遇到的许多关键问题。但作为 python 新手,我找不到任何明确的 Material 来解决这个问题。

最佳答案

您没有实现构造函数。例如:

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

关于python - 页面对象模型不工作 Selenium Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53737641/

相关文章:

python - 如何让我的 python 脚本找到 chromedriver 文件,无论它在哪里使用

selenium-webdriver - 如何在 Allure 报告中显示启动、环境、趋势和执行者的数据

python - 在 Python 中处理一个简单的工作流

python - Python 内置最大/最小函数的默认键

python - 如何导入我在 Python 中创建的类并创建该类的实例?我收到一条错误消息,指出对象不可调用

java - 打开弹出窗口后程序卡住了

selenium - Firefox 47.0 在启动 selenium webdriver 时崩溃

python - 尝试在python2.7中使用Selenium获取网站日历数据

java - 使用java在Selenium WebDriver(Selenium 2)中向上或向下滚动页面

python - 无法使用 anaconda 的 conda 包更新到 python 3.5