System.InvalidCastException : Specified cast is not valid

I’m brand new to Appium Studio, so if I’m missing something simple, please go easy on me.

Background:
I connected a real Android phone to my Mac and recorded a test for it using Appium Studio.
I took the code that Appium Studio generated and used it to create a test in Visual Studio.

Here’s a snippet of what it looks like:

protected AndroidDriver driver = null;

	DesiredCapabilities dc = new DesiredCapabilities();
	[SetUp()]
	public void SetupTest()
	{
		dc.SetCapability("reportDirectory", reportDirectory);
		dc.SetCapability("reportFormat", reportFormat);
		dc.SetCapability("testName", testName);
		dc.SetCapability(MobileCapabilityType.Udid, "118x87x9");
		driver = new AndroidDriver<AndroidElement>(new Uri("http://localhost:4723/wd/hub"), dc);
	}
    
	[Test()]
	public void TestUntitled()
	{
		driver.Swipe(564, 29, 523, 1594, 909);
		driver.FindElement(By.XPath("xpath=//*[@id='settings_button']")).Click();
            }

The driver.Swipe line works correctly and pulls down the top menu on my phone.
The settings_button then gets surrounded by a red box.
Then I receive the following error and the test fails:
System.InvalidCastException : Specified cast is not valid.

I have tried removing the ‘xpath=’ as I don’t normally use that in my VS tests.
I have tried finding the button via Id as well.

Can someone shed some light on what I’m doing wrong?

Mark

I’d recommend the following to narrow in on the issue: separate the finding of the element from the clicking of the element:

AndroidElement aElement = driver.FindElement (etc…);
aElement.click();

See which of these produces the casting exception and repair.

I do most of my work using AppiumDriver and WebElement .

Thanks for the suggestion wakeIt.

I removed the the .Click(), set the result to an AndroidElement and still received the error.

I was able to work around it by changing

new AndroidDriver<AndroidElement>

to
new RemoteWebDriver

This enabled me to click the item as desired. However, it does prevent me from performing swipe actions, so I would still like to find the root cause of the issue so I can return to the Android driver.

Mark