【问题标题】:Why find_element_by_xpath() taking 3 positional argument?为什么 find_element_by_xpath() 采用 3 个位置参数?
【发布时间】:2019-01-09 08:42:03
【问题描述】:

*args 以元组形式解包定位器。但在我的情况下,我只给出了两个参数,但它需要三个参数。需要帮助理解。

我是 python 的 selenium 新手,玩过一些来自 github 的代码, 但出现错误。

TypeError: find_element_by_xpath() 接受 2 个位置参数,但给出了 3 个

locator.py
from selenium.webdriver.common.by import By
class elements(object):
      Customer = (By.XPATH, "//button[contains(text(),'Customer')]")


base.py
from selenium import webdriver
from selenium.webdriver.common.by import By

class Page(object):
    def __init__(self,driver,url=None):
        self.url = url
        self.driver = driver

    def find_element_with_click(self,*locator):
        self.driver.find_element_by_xpath(*locator).click()


pages.py
from selenium import webdriver
from base import Page
from locator import *

class CustomerCreation(Page):
    def __init__(self, driver):
        self.locator = elements
        super().__init__(driver)

    def create_customer(self):
        self.driver.find_element_with_click(*self.locator.Customer)


testPages.py
import unittest
from selenium import webdriver
from pages import *
from locators import *
from selenium.webdriver.common.by import By

class TestPages(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome('C:\ChromeDriver\chromedriver')
        cls.driver.get("#server")


    def test_tes_cust(self):
        page = CustomerCreation(self.driver)
        res_page = page.create_customer()          #Getting issue at this stage

    @classmethod
    def tearDownClass(cls):
        cls.driver.close()


if __name__ == "__main__":
    suite = unittest.TestLoader().loadTestsFromTestCase(TestPages)
    unittest.TextTestRunner(verbosity=2).run(suite)

错误日志:

test_tes_cust (ma​​in.TestPages) ... 错误 ==================================================== ===================== 错误:test_tes_cust (ma​​in.TestPages) -------------------------------------------------- -------------------- 回溯(最近一次通话最后): 文件“testPages.py”,第 28 行,在 test_tes_cust res_page = page.create_customer() 文件“C:\Users###\PycharmProjects\basics\pages.py”,第 35 行,在 create_customer self.find_element_with_click(*self.locator.Customer) 文件“C:\Users###\PycharmProjects\basics\base.py”,第 21 行,在 find_element_with_click self.driver.find_element_by_xpath(*locator).click() TypeError: find_element_by_xpath() 接受 2 个位置参数,但给出了 3 个

【问题讨论】:

    标签: python selenium selenium-webdriver


    【解决方案1】:

    你传递了一个额外的参数。你的论点是:

    1. self
    2. By.XPATH
    3. "//button[contains(text(),'Customer')]"

    这就是您需要传递给find_element 方法的内容。而find_element_by_xpath 应该只接受两个参数:

    1. self
    2. "//button[contains(text(),'Customer')]"

    所以尝试将您的代码更新为

    def find_element_with_click(self,*locator):
        self.driver.find_element(*locator).click()
    

    或者您需要将Customer 修改为:

    Customer = "//button[contains(text(),'Customer')]"
    

    def find_element_with_click(self, xpath):
        self.driver.find_element_by_xpath(xpath).click()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      • 2021-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-06
      相关资源
      最近更新 更多