Can we find element by ID in appium

前端 未结 6 701
野的像风
野的像风 2020-12-09 04:13

following link mentions that we can find element by giving id... but i am unable to find it.

https://github.com/appium/appium/blob/master/docs/en/writing-running-app

相关标签:
6条回答
  • 2020-12-09 04:33

    Here is the jave code with most common ways to locate elements using Appium

    //Find element by id and enter Hello in Text box.
    
        driver.findElementById("edit_name").sendKeys("Hello");
        //Find element by class name and enter Hello in Text box.
    
         driver.findElementByClassName("com.android.EditText").sendKeys("Hello");
        //Find element by xpath and enter Hello in Text box.
    
        driver.findElementByXPath("//cass[@value='Enter Name']").sendKeys("Hello");
        //Find element by link text and enter Hello in Text box.
    
        driver.findElementByLinkText("Enter Name").sendKeys("Hello");
    

    If you want to learn more ways to find elements in appium for native and webviews VISIT HERE

    0 讨论(0)
  • 2020-12-09 04:37

    You can use element ID as following:-

    package name : com.example.testap

    Element ID : txtLogin

    write the following code -

     driver.findElement(By.id("com.example.testapp:id/txtLogin")).sendKeys("abc");
    
    0 讨论(0)
  • 2020-12-09 04:40

    You can find by id using Xpath too,e.g. here element has class="android.widget.Button" and id="digit5" so you can find like

    xpath("//android.widget.Button[contains(@resource-id,'digit5')]")

    Xpath is most useful when you want apply Multiple condition like xpath("//android.widget.Button[contains(@resource-id,'digit5') and @text='5']") for more details you can see on http://www.software-testing-tutorials-automation.com/2015/10/ui-automator-viewer-get-android-app.html

    0 讨论(0)
  • 2020-12-09 04:40

    Getting element by id is not possible for Android API level less than 18.Can you check in the uiautomatorviewer if the id is present. If not you can get all the elements of the type EditView and iterate over it to find the correct field to send text.

    Use it this way -

    public List<WebElement> getWebElementList(By by) {
    
            return driver.findElements(by);
        }
    
    public void details() throws InterruptedException{
    
            List<WebElement> weList = null;
            weList = getWebElementList(By.tagName("android.widget.EditView"));
            for (WebElement we : weList) {
                if(we.getText().toLowerCase().contains("login")){
                    we.sendkeys("Hello");
                }
            }           
        }
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-09 04:43

    In the Appium version 1.0 onwards, findElementByTagName is deprecated.

    You have to use the findElementByClassName instead with qualified class Name. Code for EditTextField and Button in android as below :

    findElements(By.className("android.widget.EditText"));

    findElements(By.className("android.widget.Button"));

    0 讨论(0)
  • 2020-12-09 04:47

    You can find the ID of an element using android uiautomatorviewer. just go to your SDK tools folder D:\Android\android-sdk\tools, here you can find uiautomatorviewer. Open it and take screen shots of your activity and find the id, class, package of any element.

    enter image description here

    0 讨论(0)
提交回复
热议问题