Chrome 使用 selenium 上传图片时崩溃,如何解决?

Chrome is crashing while image upload using selenium, How to fix it?

我正在使用 selenium 和 appium 在 Android phone 上自动化移动 chrome 浏览器。 我已经通过

连接了真正的 android 设备

adb connect {ip address of mobile}

android chrome 浏览器在真实设备上的所有其他测试用例 运行 成功。但是图片上传,浏览器崩溃了。

我使用下面的代码上传图片

 public class UploadPhotoPage extends TestBase {

    // Page Factory Object Repository
    @FindBy(id = "file-upload")
    WebElement fileUpload;

    // Initialising the page object
    public UploadPhotoPage() {
    PageFactory.initElements(driver, this);
    }

    // Actions
    public void uploadphotos(String imagePath) throws AWTException, InterruptedException {
    fileUpload.sendKeys(imagePath);
    }
}

图像路径在 windows 机器上,appium 服务器和 eclipse 正在执行测试用例。

这里有什么问题?

appium 服务器上的错误日志:

[W3C] Driver proxy active, passing request on via HTTP proxy [JSONWP Proxy] Matched '/wd/hub/session/34e405a5-1bad-4711-9e1c-956e2d7391f6/element/0.6272871137575717-18/text' to command name 'getText' [JSONWP Proxy] Proxying [GET /wd/hub/session/34e405a5-1bad-4711-9e1c-956e2d7391f6/element/0.6272871137575717-18/text] to [GET http://127.0.0.1:8003/wd/hub/session/adb1415c77099095355e619b36dc5e1e/element/0.6272871137575717-18/text] with body: {} [JSONWP Proxy] Got response with status 200: "{\"sessionId\":\"adb1415c77099095355e619b36dc5e1e\",\"status\":10,\"value\":{\"message\":\"stale element reference: element is not attached to the page document\n (Session info: chrome=70.0.3538.64)\n (Driver info: chromedriver=2.42.591088 (7b2b2dca23cca0862f674758c9a3933e685c27d5),platform=Windows NT 6.1.7601 SP1 x86_64)\"}}" [W3C] Encountered internal error running command: ProxyRequestError: Could not proxy command to remote server. Original error: The request to /wd/hub/session/34e405a5-1bad-4711-9e1c-956e2d7391f6/element/0.6272871137575717-18/text has failed [W3C] at JWProxy.proxy$ (C:\Users\chintamanim\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\appium-base-driver\lib\jsonwp-proxy\proxy.js:182:13) [W3C] at tryCatch (C:\Users\chints\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:67:40) [W3C] at GeneratorFunctionPrototype.invoke [as _invoke] (C:\Users\chints\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:315:22) [W3C] at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (C:\Users\chints\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:100:21) [W3C] at GeneratorFunctionPrototype.invoke (C:\Users\chints\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:136:37) [W3C] at [MJSONWP] Matched JSONWP error code 10 to StaleElementReferenceError [HTTP] <-- GET /wd/hub/session/34e405a5-1bad-4711-9e1c-956e2d7391f6/element/0.6272871137575717-18/text 404 95 ms - 2004 [HTTP] [HTTP] --> GET /wd/hub/session/34e405a5-1bad-4711-9e1c-956e2d7391f6/element/0.6272871137575717-18/text [HTTP] {} [W3C] Driver proxy active, passing request on via HTTP proxy [JSONWP Proxy] Matched '/wd/hub/session/34e405a5-1bad-4711-9e1c-956e2d7391f6/element/0.6272871137575717-18/text' to command name 'getText' [JSONWP Proxy] Proxying [GET /wd/hub/session/34e405a5-1bad-4711-9e1c-956e2d7391f6/element/0.6272871137575717-18/text] to [GET http://127.0.0.1:8003/wd/hub/session/adb1415c77099095355e619b36dc5e1e/element/0.6272871137575717-18/text] with body: {} [JSONWP Proxy] Got response with status 200: "{\"sessionId\":\"adb1415c77099095355e619b36dc5e1e\",\"status\":10,\"value\":{\"message\":\"stale element reference: element is not attached to the page document\n (Session info: chrome=70.0.3538.64)\n (Driver info: chromedriver=2.42.591088 (7b2b2dca23cca0862f674758c9a3933e685c27d5),platform=Windows NT 6.1.7601 SP1 x86_64)\"}}" [W3C] Encountered internal error running command: ProxyRequestError: Could not proxy command to remote server. Original error: The request to /wd/hub/session/34e405a5-1bad-4711-9e1c-956e2d7391f6/element/0.6272871137575717-18/text has failed [W3C] at JWProxy.proxy$ (C:\Users\chints\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\appium-base-driver\lib\jsonwp-proxy\proxy.js:182:13) [W3C] at tryCatch (C:\Users\chints\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:67:40) [W3C] at GeneratorFunctionPrototype.invoke [as _invoke] (C:\Users\chints\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:315:22) [W3C] at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (C:\Users\chints\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:100:21) [W3C] at GeneratorFunctionPrototype.invoke (C:\Users\chints\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:136:37) [W3C] at [MJSONWP] Matched JSONWP error code 10 to StaleElementReferenceError [HTTP] <-- GET /wd/hub/session/34e405a5-1bad-4711-9e1c-956e2d7391f6/element/0.6272871137575717-18/text 404 135 ms - 2004

以下是能力,我已经设置

        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
        capabilities.setCapability("deviceName", "ONEPLUS A5010");
        capabilities.setCapability("platformVersion", "8.1.0");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("browserName", "Chrome");
        capabilities.setCapability("deviceId", "XXX.XX.XX.XXX:5555");
        capabilities.setCapability("autoGrantPermissions", true);

处理 StaleElementException 的一个快速解决方案是,当我们遇到 Exception 时,再次在 DOM 中找到相同的元素 WebElement

 public class UploadPhotoPage extends TestBase {

// Page Factory Object Repository
@FindBy(id = "file-upload")
WebElement fileUpload;

// Initialising the page object
public UploadPhotoPage() {
PageFactory.initElements(driver, this);
}

// Actions
public void uploadphotos(String imagePath) throws AWTException, InterruptedException {
try {
fileUpload.sendKeys(imagePath);
}
catch(StaleElementException ste){
driver.findElement(By.id("file-upload")).sendKeys(imagePath);
}
    }
}

我的脚本问题如下 1)过时的元素问题是因为另一个元素,所以它不是根本原因。 2)图片路径应该是手机图片,而不是桌面图片。这是个大问题。