python中appium 的NoSuchElementException错误 原因以及解决办法

发布于:2025-08-05 ⋅ 阅读:(11) ⋅ 点赞:(0)

错误收集

D:\Program\Util\python.exe "D:/Program/myUtil/PyCharm 2024.3.5/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py" --target demo.py::TestAppium 
Testing started at 15:57 ...
Launching pytest with arguments demo.py::TestAppium --no-header --no-summary -q in C:\Users\Administrator\PycharmProjects\pyAppium

============================= test session starts =============================
collecting ... collected 1 item

demo.py::TestAppium::test_study 

======================== 1 failed, 3 warnings in 6.18s ========================
FAILED                                   [100%]
demo.py:15 (TestAppium.test_study)
self = <demo.TestAppium object at 0x00000298250C8B90>

    def test_study(self):
>       ele = self.app_do.find_element(by=AppiumBy.ID,value="com.microvirt.launcher2:id/tv_hint_name")
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

demo.py:17: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
D:\Program\Util\Lib\site-packages\selenium\webdriver\remote\webdriver.py:922: in find_element
    return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
D:\Program\Util\Lib\site-packages\selenium\webdriver\remote\webdriver.py:454: in execute
    self.error_handler.check_response(response)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <appium.webdriver.errorhandler.MobileErrorHandler object at 0x00000298252001A0>
response = {'status': 404, 'value': '{"value":{"error":"no such element","message":"An element could not be located on the page u...ode_modules\\\\appium\\\\node_modules\\\\@appium\\\\base-driver\\\\lib\\\\basedriver\\\\commands\\\\find.ts:75:12)"}}'}

    def check_response(self, response: Dict[str, Any]) -> None:
        """
        https://www.w3.org/TR/webdriver/#errors
        """
        payload = response.get('value', '')
        if isinstance(payload, dict):
            payload_dict = payload
        else:
            try:
                payload_dict = json.loads(payload)
            except (json.JSONDecodeError, TypeError):
                return
            if not isinstance(payload_dict, dict):
                return
        value = payload_dict.get('value')
        if not isinstance(value, dict):
            return
        error = value.get('error')
        if not error:
            return
    
        message = value.get('message', error)
        stacktrace = value.get('stacktrace', '')
        # In theory, we should also be checking HTTP status codes.
        # Java client, for example, prints a warning if the actual `error`
        # value does not match to the response's HTTP status code.
        exception_class: Type[sel_exceptions.WebDriverException] = ERROR_TO_EXC_MAPPING.get(
            error, sel_exceptions.WebDriverException
        )
        if exception_class is sel_exceptions.WebDriverException and message:
            if message == 'No such context found.':
                exception_class = appium_exceptions.NoSuchContextException
            elif message == 'That command could not be executed in the current context.':
                exception_class = appium_exceptions.InvalidSwitchToTargetException
    
        if exception_class is sel_exceptions.UnexpectedAlertPresentException:
            raise sel_exceptions.UnexpectedAlertPresentException(
                msg=message,
                stacktrace=format_stacktrace(stacktrace),
                alert_text=value.get('data'),
            )
>       raise exception_class(msg=message, stacktrace=format_stacktrace(stacktrace))
E       selenium.common.exceptions.NoSuchElementException: Message: An element could not be located on the page using the given search parameters.; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#nosuchelementexception
E       Stacktrace:
E       NoSuchElementError: An element could not be located on the page using the given search parameters.
E           at AndroidUiautomator2Driver.findElOrEls (C:\Users\Administrator\.appium\node_modules\appium-uiautomator2-driver\node_modules\appium-android-driver\lib\commands\find.ts:86:11)
E           at runMicrotasks (<anonymous>)
E           at processTicksAndRejections (node:internal/process/task_queues:96:5)
E           at AndroidUiautomator2Driver.findElOrElsWithProcessing (D:\Program\Util\nvm\nvm\v16.20.2\node_modules\appium\node_modules\@appium\base-driver\lib\basedriver\commands\find.ts:60:12)
E           at AndroidUiautomator2Driver.findElement (D:\Program\Util\nvm\nvm\v16.20.2\node_modules\appium\node_modules\@appium\base-driver\lib\basedriver\commands\find.ts:75:12)

D:\Program\Util\Lib\site-packages\appium\webdriver\errorhandler.py:125: NoSuchElementException

Process finished with exit code 1

错误分析

NoSuchElementException

使用appium定位的时候,没有定位到该元素。

解决办法

使用其他的定位方式

  • 使用ID定位
  • 检查是不是定位方式出错了
  • 使用class定位
  • 使用XPath定位
  • 或者使用名字定位
  • Css选择器定位

运行结果

使用其他中方式定位可以解决。


网站公告

今日签到

点亮在社区的每一天
去签到