gpt4 book ai didi

python - 为什么 find_element_by_xpath() 采用 3 个位置参数?

转载 作者:太空宇宙 更新时间:2023-11-04 02:08:43 25 4
gpt4 key购买 nike

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

我是 selenium 的新手,使用 python 玩过一些来自 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 (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. 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()

关于python - 为什么 find_element_by_xpath() 采用 3 个位置参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54106092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com