appium / appium-mac2-driver

Next-gen Appium macOS driver, backed by Apple XCTest
Apache License 2.0
116 stars 24 forks source link

Getting 'NoSuchElementException' for a valid element location while using Appium mac2 driver #114

Closed ArunRajaaSelvarajan closed 2 years ago

ArunRajaaSelvarajan commented 2 years ago

Hello,

I am trying to automate a mac desktop app using 'appium-mac2-driver' via PyCharm - Pytest where I am able to launch the application but not able to do any operations since I am getting 'no such element : unable to find an element' for a valid element locator. So I tried the example code given in the site to launch and sent text message to 'Text Edit' app. But still getting the same error message here as well. I have checked the element locator using 'Appium inspector' and it looks correct. Not sure where I am going wrong.

Code:

from appium.webdriver.common.appiumby import AppiumBy
from appium import webdriver

def step_impl():
    desired_caps = {
        "automationName": "Mac2",
        "platformName": "Mac",
        "bundleId": "com.apple.TextEdit",
        "showServerLogs": True
    }

    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

    edit_field = driver.find_element(by=AppiumBy.CLASS_NAME, value='XCUIElementTypeTextView')
    edit_field.send_keys('hello world')

I have completed the below requirements in my mac(Big Sur - 11.6):

  1. Xcode 13.2.1
  2. Appium - 1.22.2 (installed using 'npm install appium -g') and appium-mac2-driver (installed using 'npm install appium-mac2-driver -g')
  3. Completed drag & drop the Xcode Helper app to Security & Privacy -> Privacy -> Accessibility list of my System Preferences.

Stacktrace:

E       NoSuchElementError: unable to find an element using 'class name', value 'XCUIElementTypeTextView'
E           at errorFromW3CJsonCode (/Users/as61/.nvm/versions/node/v17.1.0/lib/node_modules/appium/node_modules/appium-base-driver/lib/protocol/errors.js:780:25)
E           at ProxyRequestError.getActualError (/Users/as61/.nvm/versions/node/v17.1.0/lib/node_modules/appium/node_modules/appium-base-driver/lib/protocol/errors.js:663:14)
E           at WDAMacProxy.command (/Users/as61/.nvm/versions/node/v17.1.0/lib/node_modules/appium/node_modules/appium-base-driver/lib/jsonwp-proxy/proxy.js:272:19)
E           at runMicrotasks (<anonymous>)
E           at processTicksAndRejections (node:internal/process/task_queues:96:5)
E           at Mac2Driver.findElOrEls (/Users/as61/.nvm/versions/node/v17.1.0/lib/node_modules/appium/node_modules/appium-mac2-driver/lib/commands/find.js:17:10)
E           at Mac2Driver.findElOrElsWithProcessing (/Users/as61/.nvm/versions/node/v17.1.0/lib/node_modules/appium/node_modules/appium-base-driver/lib/basedriver/commands/find.js:33:12)
E           at Mac2Driver.findElement (/Users/as61/.nvm/versions/node/v17.1.0/lib/node_modules/appium/node_modules/appium-base-driver/lib/basedriver/commands/find.js:53:10)

../Library/Python/3.10/lib/python/site-packages/selenium/webdriver/remote/errorhandler.py:247: NoSuchElementException

Kindly provide some guidance to sort out this issue.

mykola-mokhnach commented 2 years ago

Is the destination element present in the page source?

ArunRajaaSelvarajan commented 2 years ago

Thanks @mykola-mokhnach . I tried adding the line of code (pageSource = driver.page_source) and all of sudden the script started working as expected. That was weird. I am able to see 'hello world' being passed to Text/Edit

from appium.webdriver.common.appiumby import AppiumBy from appium import webdriver

def step_impl(): desired_caps = { "automationName": "Mac2", "platformName": "Mac", "bundleId": "com.apple.TextEdit", "showServerLogs": True }

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
pageSource = driver.page_source

edit_field = driver.find_element(by=AppiumBy.CLASS_NAME, value='XCUIElementTypeTextView')
edit_field.send_keys('hello world')

But once the run is completed I am able to see an error thrown by Appium which is causing failure. Any idea how to solve this one.

Stacktrace: E UnknownError: An unknown server-side error occurred while processing the command. Original error: Could not proxy. Proxy error: Request failed with status code 400 E at getResponseForW3CError (/Applications/Appium Server GUI.app/Contents/Resources/app/node_modules/appium/node_modules/appium-base-driver/lib/protocol/errors.js:804:9) E at asyncHandler (/Applications/Appium Server GUI.app/Contents/Resources/app/node_modules/appium/node_modules/appium-base-driver/lib/protocol/protocol.js:380:37)

../Library/Python/3.10/lib/python/site-packages/selenium/webdriver/remote/errorhandler.py:247: WebDriverException

ArunRajaaSelvarajan commented 2 years ago

driver.quit() solved the above issue. Closing this issue