Why find_element_by_xpath() taking 3 positional argument?
*args以tuple形式解包定位器。但我只给出了两个参数,但它有三个参数。需要帮助才能理解。
我对使用python的Selenium还是个新手,在Github上玩过一些代码,但是出错了。
类型错误:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | 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 (main.TestPages) ... ERROR
======================================================================
ERROR: test_tes_cust (main.TestPages)
----------------------------------------------------------------------
Traceback (most recent call last):
File"testPages.py", line 28, in test_tes_cust
res_page = page.create_customer()
File"C:\Users###\PycharmProjects\basics\pages.py", line 35, in create_customer
self.find_element_with_click(*self.locator.Customer)
File"C:\Users###\PycharmProjects\basics\base.py", line 21, in find_element_with_click
self.driver.find_element_by_xpath(*locator).click()
TypeError: find_element_by_xpath() takes 2 positional arguments but 3 were given
号
你又传了一个论点。你的论点是:
这就是您需要传递给
所以尝试将代码更新为
1 2 | def find_element_with_click(self,*locator): self.driver.find_element(*locator).click() |
或者您需要将您的
1 | Customer ="//button[contains(text(),'Customer')]" |
号
和
1 2 | def find_element_with_click(self, xpath): self.driver.find_element_by_xpath(xpath).click() |