Unable to find an element in Browser of the Android emulator using Appium and C#

两盒软妹~` 提交于 2019-12-02 18:15:26

问题


I want to automate mobile web site testing on Android emulator using c# and Appium. There is a simple test scenario I want to automate for the start:
1. Start Browser
2. Find an element
3. Clear it
4. Send keys

I've got a problem with the second step. Every time MSTest tries to execute FindElementById line in the code below, I get the error: "An element could not be located on the page using the given search parameters."

[TestClass]
public class UnitTest1
{
    private DesiredCapabilities _capabilities;
    private AndroidDriver _driver;

    public void InitializeDriver()
    {
        Console.WriteLine("Connecting to Appium server");
        _capabilities = new DesiredCapabilities();

        _capabilities.SetCapability("deviceName", "test_02");
        _capabilities.SetCapability(CapabilityType.BrowserName, "Chrome");
        _capabilities.SetCapability(CapabilityType.Version, "5.0.1");
        _capabilities.SetCapability(CapabilityType.Platform, "Android");

        //Application path and configurations
        _driver = new AndroidDriver(new Uri("http://127.0.0.1:4723/wd/hub"), _capabilities);
    }

    [TestMethod]
    public void TestMethod1()
    {
        InitializeDriver();

        var element = _driver.FindElementById("com.android.browser:id/url");
        element.Clear();
        element.SendKeys(@"http://stackoverflow.com/");
    }
}

Input string for the method I've got from UIAutomator that is shown below.

I tried several combinations for the FindElementById input method:
"com.android.browser:id/url"
"id/url"
"url"
but no luck.

My environment:
Windows 8.1
Appium 1.3.4.1
ChromeDriver 2.14.313457
Android Device Monitor 24.0.2


回答1:


Sorry for misleading !!! In case of testing web apps in browser the elements should be located as usual elements on the web page ( not as some classes like android.widget.EditText and android.widget.Button). So try for example the following and you will see some result:

    var element = _driver
            .findElementByXPath("//input[@id='lst-ib']");

To get locators you should run the browser on your desktop, open the page and use some tools/extensions like Firebug in Firefox or Firebug Lite in Chrome browser.




回答2:


Try these 2 statements:

var element = _driver.FindElement(By.Id("com.android.browser:id/url");
driver.findElementsByXPath("//*[@class='com.android.browser' and @index='1']");



回答3:


Update ! The following approach is not for web testing:

Could you try to find the element using xpath?

@FindBy(xpath="//android.widget.EditText[contains(@resource-id, 'url')]")

So in your case you can try the following:

var element = _driver.findElementByXPath("//android.widget.EditText[contains(@resource-id, 'url')]");

Update: in case of testing web apps (not native) you should use web page locators instead of Android classes.



来源:https://stackoverflow.com/questions/28808201/unable-to-find-an-element-in-browser-of-the-android-emulator-using-appium-and-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!