无法单击/选择按钮和文本字段

Posted

技术标签:

【中文标题】无法单击/选择按钮和文本字段【英文标题】:Unable to click/select the button and text field 【发布时间】:2016-06-24 09:29:24 【问题描述】:

enter image description here朋友们,

以下是我的代码,应用程序在运行时启动,但无法选择按钮,出现如下错误:

是不是因为我使用的是 RemoteWebDriver 而不是 androidDriver?

Appium 错误日志:

 > info: [debug] Responding to client with error: "status":7,"value":"message":"An element could not be located on the page using the given search parameters.","origValue":"Element was not found.","sessionId":"633dd3fe-7233-eb66-8903-c63b69246cf0"
> info: <-- POST /wd/hub/session/633dd3fe-7233-eb66-8903-c63b69246cf0/element 500 30181.879 ms - 201 

信息:--> 删除 /wd/hub/session/633dd3fe-7233-eb66-8903-c63b69246cf0 info: 关闭 appium 会话

Eclipse 错误日志:

Test Started
FAILED: test
org.openqa.selenium.NoSuchElementException: An element could not be located   on the page using the given search parameters. (WARNING: The server did not   provide any stacktrace information)
 Command duration or timeout: 30.19 seconds
 For documentation on this error, please visit:     http://seleniumhq.org/exceptions/no_such_element.html
  Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:59:20'
  System info: host: 'NCA1026471', ip: '192.168.56.1', os.name: 'Windows 7',   os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_66'
 Driver info: org.openqa.selenium.remote.RemoteWebDriver
 Capabilities [app=C:\Appium\selendroid-test-app-0.17.0.apk,     appPackage=io.selendroid.testapp, rotatable=true, networkConnectionEnabled=true,    warnings=, handlesAlerts=true, deviceName=Lokesh Gangaiah, version=0.17.0,   platform=ANDROID, appActivity=io.selendroid.testapp.HomeScreenActivity, desired=  app=C:\Appium\selendroid-test-app-0.17.0.apk, appPackage=io.selendroid.testapp,  appActivity=io.selendroid.testapp.HomeScreenActivity, platformVersion=5.0,  browserName=, platformName=Android, deviceName=Lokesh Gangaiah, device=Android, acceptSslCerts=true, platformVersion=21, automationName=selendroid,   browserName=selendroid, takesScreenshot=true, javascriptEnabled=true,  platformName=android, device=Android]
Session ID: 633dd3fe-7233-eb66-8903-c63b69246cf0
 *** Element info: Using=id,   value=io.selendroid.testapp:id/buttonStartWebview
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

java代码:

 import java.io.File;
 import java.net.URL;
 import java.util.concurrent.TimeUnit;
 import io.appium.java_client.MobileElement;
 import io.appium.java_client.android.AndroidDriver;
 import org.openqa.selenium.By;
 import org.openqa.selenium.WebElement;
 import org.openqa.selenium.mobile.*;
 import org.openqa.selenium.remote.CapabilityType;
 import org.openqa.selenium.remote.DesiredCapabilities;  
 import org.openqa.selenium.remote.RemoteWebDriver;
 import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;

 public class testP1 

 public static RemoteWebDriver driver;
//public static AndroidDriver<MobileElement> driver;

@BeforeClass
public static void setUpBeforeClass() throws Exception 

        File appDir = new File ("C:\\Appium\\");
        //File app = new File (appDir,"CarsGuide com   au_v1.0.8.1_apkpure.com.apk");
        File app = new File (appDir,"selendroid-test-app-0.17.0.apk");
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("browserName", "");
        //capabilities.setCapability(CapabilityType.BROWSER_NAME, "");

        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("deviceName", "Lokesh Gangaiah");
        capabilities.setCapability("platformVersion", "5.0");
        capabilities.setCapability("device", "Android");

        capabilities.setCapability("app",app.getAbsolutePath());
        capabilities.setCapability("appPackage", "io.selendroid.testapp");
        capabilities.setCapability("appActivity", "io.selendroid.testapp.HomeScreenActivity");

        //capabilities.setCapability("appPackage", "au.com.carsguide");
        //capabilities.setCapability("appActivity", "au.com.carsguide.activity.HomeActivity");

        driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);



@Test
public void test() throws InterruptedException 

    System.out.println("Test Started");

    WebElement ButtonElement =     driver.findElementById("io.selendroid.testapp:id/buttonStartWebview");
    ButtonElement.click();

    Thread.sleep(3000L);

    System.out.println("Test has been completed");

    //System.out.println("Hello2");


    //       driver.findElementByName("Search Cars for Sale").click();
    //By.id("au.com.carsguide:id/search_btn").findElement(driver).click();
    Thread.sleep(1000L);

   

  @AfterClass
     public void tearDown()
     
        driver.quit();
    

 

添加等待后的新错误:从 Eclipse 控制台登录

 Test Started
 FAILED: test
 org.openqa.selenium.TimeoutException: Timed out after 20 seconds waiting for visibility of element located by By.id:    io.selendroid.testapp:id/buttonStartWebview

构建信息:版本:'2.48.2',修订:'41bccdd',时间:'2015-10-09 19:59:20' 系统信息:主机:'NCA1026471',ip:'192.168.56.1',os.name:'Windows 7',os.arch:'amd64',os.version:'6.1',java.version:'1.8.0_66 ' 驱动程序信息:org.openqa.selenium.remote.RemoteWebDriver 功能 [app=C:\Appium\selendroid-test-app-0.17.0.apk, appPackage=io.selendroid.testapp, rotatable=true, networkConnectionEnabled=true, warnings=, handlesAlerts=true, deviceName=Lokesh Gangaiah,版本=0.17.0,平台=ANDROID,appActivity=io.selendroid.testapp.HomeScreenActivity,期望=app=C:\Appium\selendroid-test-app-0.17.0.apk,appPackage=io.selendroid。 testapp,appActivity=io.selendroid.testapp.HomeScreenActivity,platformVersion=5.0,browserName=,platformName=Android,deviceName=Lokesh Gangaiah,device=Android,acceptSslCerts=true,platformVersion=21,automationName=selendroid,browserName=selendroid,takeScreenshot =true, javascriptEnabled=true, platformName=android, device=Android] 会话 ID:990a6104-0d06-f75a-876c-531a23388b7c

尝试使用 AndroidDriver 得到以下错误:

 FAILED CONFIGURATION: @BeforeClass setUpBeforeClass
 java.lang.Error: Unresolved compilation problems: 
 The return types are incompatible for the inherited methods   FindsByAccessibilityId<WebElement>.findElementsByAccessibilityId(String),   AppiumDriver<RequiredElementType>.findElementsByAccessibilityId(String)
The return types are incompatible for the inherited methods   FindsByCssSelector.findElementsByCssSelector(String),   AppiumDriver<RequiredElementType>.findElementsByCssSelector(String)

【问题讨论】:

您确定您使用的是页面上提供的正确 id 吗? 是的 id 是正确的,我交叉验证了 你能分享一下你的定位器页面截图吗 是的,刚刚上传 等一下你想点击EN按钮对吗? 【参考方案1】:

在点击前添加一些等待命令:

@Test
public void test() throws InterruptedException 

    System.out.println("Test Started");

    //wait for the button to show up before clicking
    waitForElementPresent();
    WebElement ButtonElement =     driver.findElementByName("buttonStartWebviewCD");
    ButtonElement.click();

     public void waitForElementPresent() 
            WebDriverWait wait = new WebDriverWait(driver, 20);
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("buttonStartWebviewCD")));
        

【讨论】:

感谢回复,上面的方法试过了,出现以下错误: @Lokesh 你能分享你的 UIAutomator 的截图吗?这样我可以帮你。好像您使用了错误的定位器 肯定会尝试并告诉你 上面的代码有效,但是有几个问题,如果你能帮助我会很棒 我确实得到了一些澄清,在看到这篇文章后,我使用的是带有完整包的资源 ID:***.com/questions/18362400/… 它帮助我将资源 ID 更改为仅用于 button_name..然后它工作

以上是关于无法单击/选择按钮和文本字段的主要内容,如果未能解决你的问题,请参考以下文章

无法通过单击选择文本字段(Swift)

如何在单击角度7中的按钮时获取下拉列表的选定值和选定文本[重复]

无法根据选择从 UIPickerView 中删除项目

单击提交按钮时将自定义文本从选择框提交到文本区域

按 esc 后无法打开日期选择器

隐藏键盘返回按钮