首页 > 解决方案 > 预期条件间歇性失败并出现错误:TypeError: find_element() 采用 1 到 2 个位置参数,但给出了 3 个

问题描述

我在 Selenium 上遇到了这个奇怪的问题,正在寻求一些帮助。我正在使用预期条件来验证元素的存在。

问题是有时它运行良好,但有时会引发以下错误:

File "selenium_func_call.py", line 327, in is_element_clickable
  WebDriverWait(self.driver, 10).until(element)
File "python3.5/site-packages/selenium/webdriver/support/wait.py", line 71, in until
  value = method(self._driver)
File "python3.5/site-packages/selenium/webdriver/support/expected_conditions.py", line 318, in __call__
  element = visibility_of_element_located(self.locator)(driver)
File "python3.5/site-packages/selenium/webdriver/support/expected_conditions.py", line 139, in __call__
  return _element_if_visible(_find_element(driver, self.locator))
File "python3.5/site-packages/selenium/webdriver/support/expected_conditions.py", line 440, in _find_element
  return driver.find_element(*by)
TypeError: find_element() takes from 1 to 2 positional arguments but 3 were given

我对“element_to_be_clickable()”的调用是正确的。我正在传递一个元组。我认为这是我的 xpath 或其他东西的长度,但这并没有导致任何地方。我在 expected_conditions.py 中添加了一些跟踪,以验证我是否传递了正确的参数。

调用预期条件

element = EC.element_to_be_clickable((By.XPATH, element_xpath))
WebDriverWait(self.driver, 10).until(element)

来自 expected_conditions.py 的输出

这有效:

在_find_element里面

by = ('xpath', "//span[contains(text(), 'SERVICES')]/../../..")

这些不会:

在_find_element里面

by = ('xpath', "//div[text() = 'STORAGE']/../div[2]/div[contains(@class, 'ls-add-icon ')]")

在_find_element里面

by = ('xpath', "//input[@name = 'id' and @value = 'Mint_Choco_Chip']/../../../../../div/div/div[text( ) = 'STORAGE']/../div[2]/div[contains(@class, 'ls-add-icon ')]")

关于我做错了什么的任何建议都会很可爱

标签: pythonseleniumselenium-webdriver

解决方案


推荐阅读