Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将变量作为参数传递给Seleniu find_元素函数而不是硬编码参数会导致InvalidArgumentException 上下文_Python_Selenium_Selenium Webdriver_Geckodriver - Fatal编程技术网

Python 将变量作为参数传递给Seleniu find_元素函数而不是硬编码参数会导致InvalidArgumentException 上下文

Python 将变量作为参数传递给Seleniu find_元素函数而不是硬编码参数会导致InvalidArgumentException 上下文,python,selenium,selenium-webdriver,geckodriver,Python,Selenium,Selenium Webdriver,Geckodriver,我正在将selenium与python结合使用,并尝试按照以下步骤将我的PageObjects与我的代码分开,但我发现了一个我不理解的错误: from selenium.webdriver.common.by import By class MainPageLocators(object): """A class for main page locators. All main page locators should come here"&qu

我正在将selenium与python结合使用,并尝试按照以下步骤将我的
PageObjects
与我的代码分开,但我发现了一个我不理解的错误:

from selenium.webdriver.common.by import By

class MainPageLocators(object):
    """A class for main page locators. All main page locators should come here"""
    GO_BUTTON = (By.ID, 'submit') 
像上面这样做会给我一个
selenium.common.exceptions.InvalidArgumentException:Message:第1行第11列的预期值
错误

复制 我使用谷歌主页重现了这个错误:

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


def test():
    driver = webdriver.Firefox()
    driver.get('https://www.google.com')

    element = (By.NAME, "btnK")
    driver.find_element(element)


if __name__ == "__main__":
    test()
完全错误:

Traceback (most recent call last):
  File "test.py", line 14, in <module>
    test()
  File "test.py", line 10, in test
    driver.find_element(element)
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: expected value at line 1 column 11
环境 操作系统:Windows 10 浏览器:Firefox 浏览器版本:77.0.1 浏览器驱动程序版本:GeckoDriver 0.26 语言绑定版本:Python 3.6、Python selenium 3.141.0 Selenium网格版本(如适用):

壁虎河原木 如何解决此错误?

之间的差异

driver.find_element(By.NAME, "btnK")

第一个版本发送两个参数,第二个版本发送一个参数(元组)

使用
*
运算符“解包”参数:

args = (By.NAME, "btnK")
driver.find_element(*args)
driver.find_element(By.NAME, "btnK")
args = (By.NAME, "btnK")
driver.find_element(args)
args = (By.NAME, "btnK")
driver.find_element(*args)