Why find_element_by_xpath() taking 3 positional argument?

Viewed 2705

*args unpacks the locators in tuple form.but is my case i have given only two arguments,but it is taking three arguments.Need help to understand.

I am newbie to selenium with python and played around some code from github, but getting error.

TypeError: find_element_by_xpath() takes 2 positional arguments but 3 were given

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)

The error log:

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 Answers

You're passing one extra argument. Your arguments are:

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

That's what you need to pass to find_element method. While find_element_by_xpath should take two arguments only:

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

So try to update your code as

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

or you need to modify your Customer as:

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

and

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