构建函数以清除字段和send_keys

时间:2016-11-30 21:50:26

标签: python python-2.7 selenium selenium-webdriver automated-tests

我正在尝试构建一个清除字段和send_keys的函数,但是我收到了错误。看我的代码:

base.py

class Page(object):

    def clear_sendkeys(self, *locator, text):

        self.driver.find_element(*locator).clear()
        self.driver.find_element(*locator).send_keys(text)

contractors_page.py

from selenium import webdriver
from base import Page
from locators import *
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import Select
from faker import Faker
import os
import time

def update_contacts_form(self):

    enter_building = fake.building_number()

    self.clear_sendkeys(*ContractorsLocators.EMAIL, enter_building)

我的错误

  

E self.clear_sendkeys(* ContractorsLocators.EMAIL,enter_building)   E SyntaxError:只有命名参数可以跟*表达式

3 个答案:

答案 0 :(得分:1)

要解决您的问题,您可以按如下方式更改函数参数的顺序:

def clear_sendkeys(self, text, *locator):
    self.driver.find_element(*locator).clear()
    self.driver.find_element(*locator).send_keys(text)

并称之为:

self.clear_sendkeys(enter_building, *ContractorsLocators.EMAIL)

答案 1 :(得分:1)

你不能在变量参数之后修改参数,这意味着 def clear_sendkeys(self, *locator, text)应该是错的 def clear_sendkeys(self, text, *locator)

答案 2 :(得分:0)

方法的定义如下:Traceback (most recent call last): File "C:\Loopy\lib\site-packages\IPython\core\interactiveshell.py", line 2827, in run_code exec code_obj in self.user_global_ns, self.user_ns File "<ipython-input-4-85741da8f45c>", line 1, in <module> model.add(Dense(15, init='uniform', activation='relu')) TypeError: __init__() takes at least 3 arguments (4 given)

def clear_sendkeys(self, *locator, text):抓住位置参数。所以在调用你的方法时,你不能在第一个参数之后传递更多的位置参数。

只需将最后一个参数作为关键字/命名参数传递(使用*locator前缀),如错误消息所示:

text=