Webdriver点击不适用于Android设备上的chrome

时间:2016-08-17 12:48:56

标签: selenium-webdriver appium selenium-chromedriver

我正在尝试使用Selenium webdriver和appium在Android设备上自动化Web应用程序。但是因为click()似乎不适用于Android设备上的Chrome浏览器,因此同样适用于桌面上的Chrome浏览器。

请参阅随附的测试代码,以便在gmail.com和appium控制台日志中重现该问题。不确定这是chromeDriver的问题还是需要以其他方式实现。

代码

import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestMobileBrowser {

    public DesiredCapabilities capabilities;
    public RemoteWebDriver driver;

@BeforeTest
public void beforeTest() throws MalformedURLException{
    capabilities = new DesiredCapabilities();
    capabilities.setCapability("platformName", "Android");
    capabilities.setCapability("deviceName", "Android");
    capabilities.setCapability("browserName", "chrome");
    capabilities.setCapability("newCommandTimeout", 60);
    driver = new RemoteWebDriver(new URL("http://localhost:4723/wd/hub"), capabilities);    
}

@Test
public void test(){
    driver.get("http://gmail.com");
    driver.findElement(By.id("Email")).sendKeys("test");
    driver.findElement(By.id("next")).click();  
    driver.findElement(By.id("Passwd")).sendKeys("test");
    driver.findElement(By.id("signIn")).click();    
}

}

Appium记录

> info: --> POST /wd/hub/session {"desiredCapabilities":{"newCommandTimeout":60,"browserName":"chrome","platformName":"Android","deviceName":"Android"}}
> info: Client User-Agent string: Apache-HttpClient/4.5.1 (Java/1.8.0_60)
> info: Set mode: Proxying straight through to Chromedriver
> info: [debug] Looks like we want chrome on android
> info: [debug] Creating new appium session b061da72-cfb4-4d06-8595-b9fe52439d37
> info: [debug] Checking whether adb is present
> info: [debug] Using adb from D:\eGain\Automation\Android\sdk\platform-tools\adb.exe
> info: [debug] Using fast reset? true
> info: [debug] Preparing device for session
> info: [debug] Not checking whether app is present since we are assuming it's already on the device
> info: Retrieving device
> info: [debug] Trying to find a connected android device
> info: [debug] Getting connected devices...
> info: [debug] executing cmd: D:\eGain\Automation\Android\sdk\platform-tools\adb.exe devices
> info: [debug] 1 device(s) connected
> info: Found device a33124a7
> info: [debug] Setting device id to a33124a7
> info: [debug] Waiting for device to be ready and to respond to shell commands (timeout = 5)
> info: [debug] executing cmd: D:\eGain\Automation\Android\sdk\platform-tools\adb.exe -s a33124a7 wait-for-device
> info: [debug] executing cmd: D:\eGain\Automation\Android\sdk\platform-tools\adb.exe -s a33124a7 shell "echo 'ready'"
> info: [debug] Starting logcat capture
> info: [debug] Pushing unlock helper app to device...
> info: [debug] executing cmd: D:\eGain\Automation\Android\sdk\platform-tools\adb.exe -s a33124a7 install "C:\Program Files (x86)\Appium\node_modules\appium\build\unlock_apk\unlock_apk-debug.apk"
> info: [debug] executing cmd: D:\eGain\Automation\Android\sdk\platform-tools\adb.exe -s a33124a7 shell "dumpsys window"
> info: Unlocking screen
> info: [debug] Screen is locked, trying to unlock
> info: [debug] Getting device API level
> info: [debug] executing cmd: D:\eGain\Automation\Android\sdk\platform-tools\adb.exe -s a33124a7 shell "getprop ro.build.version.sdk"
> info: [debug] Device is at API Level 23
> info: [debug] executing cmd: D:\eGain\Automation\Android\sdk\platform-tools\adb.exe -s a33124a7 shell "am start -S -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000 -n io.appium.unlock/.Unlock"
> info: [debug] executing cmd: D:\eGain\Automation\Android\sdk\platform-tools\adb.exe -s a33124a7 shell "dumpsys window"
> info: [debug] Screen is unlocked, continuing
> info: [debug] Forwarding system:4724 to device:4724
> info: [debug] executing cmd: D:\eGain\Automation\Android\sdk\platform-tools\adb.exe -s a33124a7 forward tcp:4724 tcp:4724
> info: [debug] Pushing appium bootstrap to device...
> info: [debug] executing cmd: D:\eGain\Automation\Android\sdk\platform-tools\adb.exe -s a33124a7 push "C:\\Program Files (x86)\\Appium\\node_modules\\appium\\build\\android_bootstrap\\AppiumBootstrap.jar" /data/local/tmp/
> info: Starting App
> info: [debug] Attempting to kill all 'uiautomator' processes
> info: [debug] Getting all processes with 'uiautomator'
> info: [debug] executing cmd: D:\eGain\Automation\Android\sdk\platform-tools\adb.exe -s a33124a7 shell "ps 'uiautomator'"
> info: [debug] No matching processes found
> info: [debug] Running bootstrap
> info: [debug] spawning: D:\eGain\Automation\Android\sdk\platform-tools\adb.exe -s a33124a7 shell uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap -e pkg com.android.chrome -e disableAndroidWatchers false
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
> info: [debug] [UIAUTOMATOR STDOUT] io.appium.android.bootstrap.Bootstrap:
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 1
> info: [debug] [BOOTSTRAP] [debug] Socket opened on port 4724
> info: [debug] [BOOTSTRAP] [debug] Appium Socket Server Ready
> info: [debug] [BOOTSTRAP] [debug] Loading json...
> info: [debug] [BOOTSTRAP] [debug] Registered crash watchers.
> info: [debug] Pushing command to appium work queue: ["getDataDir",{}]
> info: [debug] [BOOTSTRAP] [debug] Client connected
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"getDataDir","params":{}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: getDataDir
> info: [debug] dataDir set to: /data/local/tmp
> info: Chromedriver: Changed state to 'starting'
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":0,"value":"\/data\/local\/tmp"}
> info: Chromedriver: Set chromedriver binary as: C:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win\chromedriver.exe
> info: Chromedriver: Killing any old chromedrivers, running: FOR /F "usebackq tokens=5" %a in (`netstat -nao ^| findstr /R /C:"9515 "`) do (FOR /F "usebackq" %b in (`TASKLIST /FI "PID eq %a" ^| findstr /I chromedriver.exe`) do (IF NOT %b=="" TASKKILL /F /PID %a))
> info: Chromedriver: No old chromedrivers seemed to exist
> info: Chromedriver: Spawning chromedriver with: C:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win\chromedriver.exe --url-base=wd/hub --port=9515
> info: Chromedriver: [STDOUT] Starting ChromeDriver 2.23.409699 (49b0fa931cda1caad0ae15b7d1b68004acd05129) on port 9515
> Only local connections are allowed.
> info: JSONWP Proxy: Proxying [GET /status] to [GET http://127.0.0.1:9515/wd/hub/status] with no body
> info: JSONWP Proxy: Got response with status 200: "{\"sessionId\":\"\",\"status\":0,\"value\":{\"build\":{\"version\":\"alpha\"},\"os\":{\"arch\":\"x86_64\",\"name\":\"Windows NT\",\"version\":\"10.0.10586\"}}}"
> info: JSONWP Proxy: Proxying [POST /session] to [POST http://127.0.0.1:9515/wd/hub/session] with body: {"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.android.chrome","androidDeviceSerial":"a33124a7"}}}
> info: JSONWP Proxy: Got response with status 200: {"sessionId":"0ebf4514a9760982649f45ce431656e8","status":0,"value":{"acceptSslCerts":true,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"browserName":"chrome","chrome":{"chromedr...
> info: Chromedriver: Changed state to 'online'
> info: [debug] Overriding session id with "0ebf4514a9760982649f45ce431656e8"
> info: [debug] Device launched! Ready for commands
> info: [debug] Setting command timeout to 60 secs
> info: [debug] Appium session started with sessionId 0ebf4514a9760982649f45ce431656e8
> info: <-- POST /wd/hub/session 303 18667.504 ms - 70 
> info: --> GET /wd/hub/session/0ebf4514a9760982649f45ce431656e8 {}
> info: JSONWP Proxy: Proxying [GET /wd/hub/session/0ebf4514a9760982649f45ce431656e8] to [GET http://127.0.0.1:9515/wd/hub/session/0ebf4514a9760982649f45ce431656e8] with body: {}
> info: JSONWP Proxy: Got response with status 200: {"sessionId":"0ebf4514a9760982649f45ce431656e8","status":0,"value":{"acceptSslCerts":true,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"browserName":"chrome","chrome":{"chromedr...
> info: JSONWP Proxy: Replacing sessionId 0ebf4514a9760982649f45ce431656e8 with 0ebf4514a9760982649f45ce431656e8
> info: <-- GET /wd/hub/session/0ebf4514a9760982649f45ce431656e8 200 11.638 ms - 606 
> info: --> POST /wd/hub/session/0ebf4514a9760982649f45ce431656e8/url {"url":"http://gmail.com"}
> info: JSONWP Proxy: Proxying [POST /wd/hub/session/0ebf4514a9760982649f45ce431656e8/url] to [POST http://127.0.0.1:9515/wd/hub/session/0ebf4514a9760982649f45ce431656e8/url] with body: {"url":"http://gmail.com"}
> info: JSONWP Proxy: Got response with status 200: {"sessionId":"0ebf4514a9760982649f45ce431656e8","status":0,"value":null}
> info: JSONWP Proxy: Replacing sessionId 0ebf4514a9760982649f45ce431656e8 with 0ebf4514a9760982649f45ce431656e8
> info: <-- POST /wd/hub/session/0ebf4514a9760982649f45ce431656e8/url 200 2374.402 ms - 72 
> info: --> POST /wd/hub/session/0ebf4514a9760982649f45ce431656e8/element {"using":"id","value":"Email"}
> info: JSONWP Proxy: Proxying [POST /wd/hub/session/0ebf4514a9760982649f45ce431656e8/element] to [POST http://127.0.0.1:9515/wd/hub/session/0ebf4514a9760982649f45ce431656e8/element] with body: {"using":"id","value":"Email"}
> info: JSONWP Proxy: Got response with status 200: {"sessionId":"0ebf4514a9760982649f45ce431656e8","status":0,"value":{"ELEMENT":"0.46630814854548563-1"}}
> info: JSONWP Proxy: Replacing sessionId 0ebf4514a9760982649f45ce431656e8 with 0ebf4514a9760982649f45ce431656e8
> info: <-- POST /wd/hub/session/0ebf4514a9760982649f45ce431656e8/element 200 154.429 ms - 103 
> info: --> POST /wd/hub/session/0ebf4514a9760982649f45ce431656e8/element/0.46630814854548563-1/value {"id":"0.46630814854548563-1","value":["test"]}
> info: JSONWP Proxy: Proxying [POST /wd/hub/session/0ebf4514a9760982649f45ce431656e8/element/0.46630814854548563-1/value] to [POST http://127.0.0.1:9515/wd/hub/session/0ebf4514a9760982649f45ce431656e8/element/0.46630814854548563-1/value] with body: {"id":"0.46630814854548563-1","value":["test"]}
> info: JSONWP Proxy: Got response with status 200: {"sessionId":"0ebf4514a9760982649f45ce431656e8","status":0,"value":null}
> info: JSONWP Proxy: Replacing sessionId 0ebf4514a9760982649f45ce431656e8 with 0ebf4514a9760982649f45ce431656e8
> info: <-- POST /wd/hub/session/0ebf4514a9760982649f45ce431656e8/element/0.46630814854548563-1/value 200 300.313 ms - 72 
> info: --> POST /wd/hub/session/0ebf4514a9760982649f45ce431656e8/element {"using":"id","value":"next"}
> info: JSONWP Proxy: Proxying [POST /wd/hub/session/0ebf4514a9760982649f45ce431656e8/element] to [POST http://127.0.0.1:9515/wd/hub/session/0ebf4514a9760982649f45ce431656e8/element] with body: {"using":"id","value":"next"}
> info: JSONWP Proxy: Got response with status 200: {"sessionId":"0ebf4514a9760982649f45ce431656e8","status":0,"value":{"ELEMENT":"0.46630814854548563-2"}}
> info: JSONWP Proxy: Replacing sessionId 0ebf4514a9760982649f45ce431656e8 with 0ebf4514a9760982649f45ce431656e8
> info: <-- POST /wd/hub/session/0ebf4514a9760982649f45ce431656e8/element 200 160.963 ms - 103 
> info: --> POST /wd/hub/session/0ebf4514a9760982649f45ce431656e8/element/0.46630814854548563-2/click {"id":"0.46630814854548563-2"}
> info: JSONWP Proxy: Proxying [POST /wd/hub/session/0ebf4514a9760982649f45ce431656e8/element/0.46630814854548563-2/click] to [POST http://127.0.0.1:9515/wd/hub/session/0ebf4514a9760982649f45ce431656e8/element/0.46630814854548563-2/click] with body: {"id":"0.46630814854548563-2"}
> info: JSONWP Proxy: Got response with status 200: {"sessionId":"0ebf4514a9760982649f45ce431656e8","status":0,"value":null}
> info: JSONWP Proxy: Replacing sessionId 0ebf4514a9760982649f45ce431656e8 with 0ebf4514a9760982649f45ce431656e8
> info: <-- POST /wd/hub/session/0ebf4514a9760982649f45ce431656e8/element/0.46630814854548563-2/click 200 350.330 ms - 72 
> info: --> POST /wd/hub/session/0ebf4514a9760982649f45ce431656e8/element {"using":"id","value":"Passwd"}
> info: JSONWP Proxy: Proxying [POST /wd/hub/session/0ebf4514a9760982649f45ce431656e8/element] to [POST http://127.0.0.1:9515/wd/hub/session/0ebf4514a9760982649f45ce431656e8/element] with body: {"using":"id","value":"Passwd"}
> info: JSONWP Proxy: Got response with status 200: {"sessionId":"0ebf4514a9760982649f45ce431656e8","status":7,"value":{"message":"no such element: Unable to locate element: {\"method\":\"id\",\"selector\":\"Passwd\"}\n  (Session info: chrome=52.0.2...
> info: JSONWP Proxy: Replacing sessionId 0ebf4514a9760982649f45ce431656e8 with 0ebf4514a9760982649f45ce431656e8
> info: <-- POST /wd/hub/session/0ebf4514a9760982649f45ce431656e8/element 200 227.600 ms - 331 
> info: [debug] Didn't get a new command in 60 secs, shutting down...
> info: Shutting down appium session
> info: Chromedriver: Changed state to 'stopping'
> info: JSONWP Proxy: Proxying [DELETE /] to [DELETE http://127.0.0.1:9515/wd/hub/session/0ebf4514a9760982649f45ce431656e8] with no body
> info: JSONWP Proxy: Got response with status 200: "{\"sessionId\":\"0ebf4514a9760982649f45ce431656e8\",\"status\":0,\"value\":null}"
> info: Chromedriver: Changed state to 'stopped'
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"shutdown"}
> info: [debug] [BOOTSTRAP] [debug] Got command of type SHUTDOWN
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":0,"value":"OK, shutting down"}
> info: [debug] [BOOTSTRAP] [debug] Closed client connection
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=.
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 0
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
> info: [debug] [UIAUTOMATOR STDOUT] Test results for WatcherResultPrinter=.
> info: [debug] [UIAUTOMATOR STDOUT] Time: 76.13
> info: [debug] [UIAUTOMATOR STDOUT] OK (1 test)
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: -1
> info: [debug] Sent shutdown command, waiting for UiAutomator to stop...
> info: [debug] UiAutomator shut down normally
> info: [debug] executing cmd: D:\eGain\Automation\Android\sdk\platform-tools\adb.exe -s a33124a7 shell "am force-stop com.android.chrome"
> info: [debug] Stopping logcat capture
> info: [debug] Logcat terminated with code null, signal SIGTERM
> info: [debug] Cleaning up appium session
> info: [debug] We shut down because no new commands came in

4 个答案:

答案 0 :(得分:2)

@Vaibhav因为在ID和密码字段输入之间有屏幕转换,appium在显示下一个屏幕之前开始寻找密码字段,所以我建议你为它添加一个等待条件。

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//android.widget.EditText[@resource-id='Passwd']")));

以下功能对我有用 -

public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "Galaxy");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Chrome");
capabilities.setCapability("platformVersion", "6.0");
capabilities.setCapability("appPackage", "com.android.chrome");
capabilities.setCapability("appActivity","com.google.android.apps.chrome.ChromeTabbedActivity");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

}

但是如果你能够输入gmail用户名并点击&#34; next&#34;按钮使用您的功能,然后您可以使用您现有的功能。只需在单击密码之前添加wait语句。

答案 1 :(得分:2)

以下代码适用于我

这是代码

public class TestMobileBrowser {

public DesiredCapabilities capabilities;
public AppiumDriver driver;
@BeforeTest
public void beforeTest() throws Exception{

capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "Android");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Chrome");
capabilities.setCapability("platformVersion", "5.0.2");
capabilities.setCapability("appPackage", "com.android.chrome");
capabilities.setCapability("appActivity","com.google.android.apps.chrome.ChromeTabbedActivity");
driver= new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}

@Test
public void test(){
driver.get("http://gmail.com");
driver.findElement(By.id("Email")).sendKeys("test");
Thread.sleep(30000);
driver.findElement(By.id("next")).click();  
driver.findElement(By.id("Passwd")).sendKeys("test");
driver.findElement(By.id("signIn")).click();    
}

}

答案 2 :(得分:0)

capabilities.setCapability("deviceName", "YOUR-DEVICE-NAME"); // such as GalaxyS6
capabilities.setCapability("browserName", "Chrome"); // its Chrome instead of chrome

您可能需要添加: chromedriverExecutable功能。 webdriver可执行文件的绝对本地路径(如果Chromium embedder提供自己的webdriver,则应该使用它而不是与Appium捆绑的原始chromedriver)

无法找到密码字段。如果它被设备键盘隐藏,我建议先隐藏键盘。

driver.hideKeyboard();
driver.findElement(By.id("Passwd")).sendKeys("test");

答案 3 :(得分:0)

driver = new RemoteWebDriver(new URL("http://localhost:4723/wd/hub"), capabilities);此行可能是错误原因。在其他答案中使用AndroidDriver和appium驱动程序。 RemoteWebDriver

中可能无法使用这些驱动程序的某些方法