How can I automate mobile web browser interaction on a real Android device?

I’m trying to automate some routine administrative tasks on my Archer AX55 WiFi router by means of Selenium WebDriver, which simulates user interaction through the router’s web-interface (unfortunately, this router doesn’t provide SSH, REST or similar API which I could use for scripting as an alternative). As far as I run the following piece of Java code on my Windows desktop system, everything works fine:

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class SeleniumDriver {
    public static void main(String[] args) throws InterruptedException {

        // compatible with Selenium 4.24.0 
        FirefoxOptions options = new FirefoxOptions();
        options.addArguments("-headless");
        WebDriver driver = new FirefoxDriver(options);

        // logging in on the router web page
        driver.get("http://192.168.0.1/webpages/index.html#/login");
        Wait<WebDriver> wait = new WebDriverWait(driver, Duration.ofSeconds(5));
        WebElement password = wait.until(webDriver -> driver.findElement(By.xpath("//input[@type='password']")));
        WebElement loginBtn = wait.until(webDriver -> driver.findElement(By.xpath("//button[@class='login-btn']")));
        password.sendKeys("<MY_LOCAL_PASSWORD>");
        loginBtn.click();
        wait.until(webDriver -> driver.findElement(By.xpath("//div[@data='advancedTopMenuBtn']")));

        // continue interaction
        
        driver.quit();
    }
}

However, when embedding the same piece of code in MainActivity class and running it from my Android phone device, I’m getting this kind of error – “java.lang.RuntimeException: Unable to find a free port at org.openqa.selenium.net.PortProber.findFreePort(PortProber.java:61)“. Switching to ChromeDriver doesn’t help.

Failed to find a solution to this problem, I’ve come to conclusion that Selenium just doesn’t provide mobile drivers to run standalone automated scripts packaged as apps on a real Android device. So, the question is the same as in the title of this topic: How can I automate mobile web browser interaction on a real Android device? Could you, please, guide me through possible solutions to this problem (if they do exist).

As an answer to your question “How can I automate mobile web browser interaction on a real Android device? “

Selenium wouldn’t directly work with your web browser on mobile, you would have to make a connection first using Appium (https://appium.io/docs/en/2.1/quickstart/test-java/) which is an open source test automation framework for mobile devices.

Aside from that, firstly you need to enable developer options and USB debugging in your mobile device.

Then install the Selenium Webdriver, Appium Webdriver, Selenium chrome driver and selenium support packages. Also remember that the driver version and your chrome version should be the same for it to work.

I did this in C# so I am attaching my code as reference to what configurations you might have to add in java

For the device name, install the adb debugger in your pc, then connect your mobile device and open a terminal and type adb devices to list all connected devices and use the device name in the appium options.

Now open appium(let the default port and host stay) and go to Advanced Settings, and add the Chrome driver path as the Chrome Driver binary path
Appium UI
Appium Options:

AppiumOptions options = new AppiumOptions();
options.AddAdditionalCapability("deviceName", "6f476994");
options.AddAdditionalCapability("platformVersion", "14");
options.AddAdditionalCapability("platformName", "Android");
options.AddAdditionalCapability("browserName", "Chrome");

Chrome Options:

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.UseSpecCompliantProtocol = false;
options.GetMergeResult(chromeOptions);

Then you can proceed with your code normally

    var driver = new RemoteWebDriver(new Uri("http://127.0.0.1:4723/wd/hub"), options);
    Thread.Sleep(3000);
    driver.Url = "https://adactinhotelapp.com/index.php";
    Thread.Sleep(5000);
    driver.FindElementById("username").SendKeys("");
    driver.FindElementById("password").SendKeys("");
    driver.FindElementById("login").Click();
 

Finally when you run the code, it should perform the relevant actions in your chrome web browser on android.

New contributor

Wahaj Javed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

How can I automate mobile web browser interaction on a real Android device?

I’m trying to automate some routine administrative tasks on my Archer AX55 WiFi router by means of Selenium WebDriver, which simulates user interaction through the router’s web-interface (unfortunately, this router doesn’t provide SSH, REST or similar API which I could use for scripting as an alternative). As far as I run the following piece of Java code on my Windows desktop system, everything works fine:

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class SeleniumDriver {
    public static void main(String[] args) throws InterruptedException {

        // compatible with Selenium 4.24.0 
        FirefoxOptions options = new FirefoxOptions();
        options.addArguments("-headless");
        WebDriver driver = new FirefoxDriver(options);

        // logging in on the router web page
        driver.get("http://192.168.0.1/webpages/index.html#/login");
        Wait<WebDriver> wait = new WebDriverWait(driver, Duration.ofSeconds(5));
        WebElement password = wait.until(webDriver -> driver.findElement(By.xpath("//input[@type='password']")));
        WebElement loginBtn = wait.until(webDriver -> driver.findElement(By.xpath("//button[@class='login-btn']")));
        password.sendKeys("<MY_LOCAL_PASSWORD>");
        loginBtn.click();
        wait.until(webDriver -> driver.findElement(By.xpath("//div[@data='advancedTopMenuBtn']")));

        // continue interaction
        
        driver.quit();
    }
}

However, when embedding the same piece of code in MainActivity class and running it from my Android phone device, I’m getting this kind of error – “java.lang.RuntimeException: Unable to find a free port at org.openqa.selenium.net.PortProber.findFreePort(PortProber.java:61)“. Switching to ChromeDriver doesn’t help.

Failed to find a solution to this problem, I’ve come to conclusion that Selenium just doesn’t provide mobile drivers to run standalone automated scripts packaged as apps on a real Android device. So, the question is the same as in the title of this topic: How can I automate mobile web browser interaction on a real Android device? Could you, please, guide me through possible solutions to this problem (if they do exist).

As an answer to your question “How can I automate mobile web browser interaction on a real Android device? “

Selenium wouldn’t directly work with your web browser on mobile, you would have to make a connection first using Appium (https://appium.io/docs/en/2.1/quickstart/test-java/) which is an open source test automation framework for mobile devices.

Aside from that, firstly you need to enable developer options and USB debugging in your mobile device.

Then install the Selenium Webdriver, Appium Webdriver, Selenium chrome driver and selenium support packages. Also remember that the driver version and your chrome version should be the same for it to work.

I did this in C# so I am attaching my code as reference to what configurations you might have to add in java

For the device name, install the adb debugger in your pc, then connect your mobile device and open a terminal and type adb devices to list all connected devices and use the device name in the appium options.

Now open appium(let the default port and host stay) and go to Advanced Settings, and add the Chrome driver path as the Chrome Driver binary path
Appium UI
Appium Options:

AppiumOptions options = new AppiumOptions();
options.AddAdditionalCapability("deviceName", "6f476994");
options.AddAdditionalCapability("platformVersion", "14");
options.AddAdditionalCapability("platformName", "Android");
options.AddAdditionalCapability("browserName", "Chrome");

Chrome Options:

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.UseSpecCompliantProtocol = false;
options.GetMergeResult(chromeOptions);

Then you can proceed with your code normally

    var driver = new RemoteWebDriver(new Uri("http://127.0.0.1:4723/wd/hub"), options);
    Thread.Sleep(3000);
    driver.Url = "https://adactinhotelapp.com/index.php";
    Thread.Sleep(5000);
    driver.FindElementById("username").SendKeys("");
    driver.FindElementById("password").SendKeys("");
    driver.FindElementById("login").Click();
 

Finally when you run the code, it should perform the relevant actions in your chrome web browser on android.

New contributor

Wahaj Javed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật