Why i am not getting the text of a link in a webview

Hi all,

Can someone please help me how to get text of a link in webview?

In APIDemos application, we have navigate to Views->WebView. Where i wanted to capture, the text name displayed. For that i wrote below code, but no text name is getting printed. Able to print webview.

What i am missing here?

public class switchToWebView 
{
	public static void main(String[] args) 
	{
		AndroidDriver driver=null;
		DesiredCapabilities caps=new DesiredCapabilities();
		caps.setCapability("deviceName", "emulator-5554");
		caps.setCapability("appPackage", "com.example.android.apis");
		caps.setCapability("appActivity", ".ApiDemos");
		
		try 
		{
			driver=new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),caps);
		} catch (MalformedURLException e) 
		{
			e.printStackTrace();
		}
		
		driver.scrollTo("Views").click();
		driver.scrollTo("WebView").click();
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		driver.context("WEBVIEW");
		System.out.println(driver.getContext());
		WebElement ele=driver.findElementByXPath("/html/body/a");
		System.out.println(ele.getText());
		driver.quit();
	}
}

did u check appium server logs may be you got something …

0 elements found by locator strategy xpath for value “/html/body/a”

how did u get this xpath for a text element ?

@amitjaincoer191, apidemo apps is sample application i do not know what is targeted api when this app is built? If it is <4.4.4 then UIAutomator will not work hence it will not locate any elements in web view. To do this, we need to build apidemo app locally and setWebviewDebug enable flag, so we can locate elements in chrome.

In Google chrome, i have given URL path as “chrome://inspect/devices#devices” and then inspect.

There is only a single element in that view, So most probably the xpath may be correct. I am not getting any “NoSuchElementException”

So can you please introduce some delay after changing context from native to web view.

Priyank,

I hope this is not a synchronization issue(wait). I kept enough wait and got the same issue.

I even provided the full context name, “WEBVIEW_com.example.android.apis”, didnt help
configured “setWebviewDebug” desired capability, didnt help.

Infact i see couple of other ChromeDriver issues. Need to figure it out how to overcome this?

setWebContentDebuging is not appium capability.

  1. Screen shot about code change in APIDemo app, where i have enabled webview

  2. When web view is actually enabled, you would be able to locate element in chrome://inspect

Thanks,
Priyank Shah

excluding “setWebContentDebuging” DC didnt help me.
So i just tried with that as a DC, but didnt help.

By chance did you get a solution to this problem?

I didnt get your first screenshot. Did you mean, you updated your APIDemo application code?

FYI… i did remote debugging as you specified in the 2nd screenshot only which i have mentioned in my my previous post

Yes, I have updated application code.

I tried your code with my custom built app and i was able to switch to WebContext. Please find below my appium server logs

[MJSONWP] Calling AppiumDriver.setContext() with args: [“WEBVIEW”,“5edc2707-fe06-4e4a-a343-ab266f554822”]
[debug] [AndroidDriver] Getting a list of available webviews
[debug] [ADB] Getting connected devices…
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /android-sdk/platform-tools/adb with args: ["-P",5037,"-s",“ZX1D62B2JM”,“shell”,“cat”,"/proc/net/unix"]
[debug] [AndroidDriver] WEBVIEW_15249 mapped to pid 15249
[debug] [AndroidDriver] Getting process name for webview
[debug] [ADB] Getting connected devices…
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running /android-sdk/platform-tools/adb with args: ["-P",5037,"-s",“ZX1D62B2JM”,“shell”,“ps”]
[debug] [AndroidDriver] Parsed pid: 15249 pkg: com.example.android.apis!
[debug] [AndroidDriver] from: u0_a630,15249,353,1029252,66024,ffffffff,00000000,R,com.example.android.apis
[debug] [AndroidDriver] returning process name: com.example.android.apis
[debug] [AndroidDriver] Found webviews: [“WEBVIEW_com.example.android.apis”]
[debug] [AndroidDriver] Available contexts: [“NATIVE_APP”,“WEBVIEW_com.example.android.apis”]
[debug] [AndroidDriver] Connecting to chrome-backed webview context ‘WEBVIEW_com.example.android.apis’
[debug] [Chromedriver] Changed state to ‘starting’
[Chromedriver] Set chromedriver binary as: /usr/local/lib/node_modules/appium/node_modules/appium-android-driver/node_modules/appium-chromedriver/chromedriver/mac/chromedriver
[Chromedriver] Killing any old chromedrivers, running: ps -ef | grep /usr/local/lib/node_modules/appium/node_modules/appium-android-driver/node_modules/appium-chromedriver/chromedriver/mac/chromedriver | grep -v grep |grep -e ‘–port=9515(\s.*)?$’ | awk ‘{ print $2 }’ | xargs kill -15
[Chromedriver] Successfully cleaned up old chromedrivers
[Chromedriver] Spawning chromedriver with: /usr/local/lib/node_modules/appium/node_modules/appium-android-driver/node_modules/appium-chromedriver/chromedriver/mac/chromedriver --url-base=wd/hub --port=9515 --adb-port=5037
[Chromedriver] [STDOUT] Starting ChromeDriver 2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4) on port 9515
Only local connections are allowed.
[JSONWP Proxy] Proxying [GET /status] to [GET http://127.0.0.1:9515/wd/hub/status] with no body
[JSONWP Proxy] Proxying [GET /status] to [GET http://127.0.0.1:9515/wd/hub/status] with no body
[JSONWP Proxy] Got response with status 200: “{“sessionId”:”",“status”:0,“value”:{“build”:{“version”:“alpha”},“os”:{“arch”:“x86_64”,“name”:“Mac OS X”,“version”:“10.11.4”}}}"
[JSONWP Proxy] Proxying [POST /session] to [POST http://127.0.0.1:9515/wd/hub/session] with body: {“desiredCapabilities”:{“chromeOptions”:{“androidPackage”:“com.example.android.apis”,“androidUseRunningApp”:true,“androidDeviceSerial”:“ZX1D62B2JM”}}}
[JSONWP Proxy] Got response with status 200: {“sessionId”:“690190e7735af36a2d524a0bbf89f15d”,“status”:0,“value”:{“acceptSslCerts”:true,“applicationCacheEnabled”:false,“browserConnectionEnabled”:false,“browserName”:“chrome”,“chrome”:{“chromedr…
[debug] [Chromedriver] Changed state to ‘online’
[MJSONWP] Responding to client with driver.setContext() result: null
[HTTP] <-- POST /wd/hub/session/5edc2707-fe06-4e4a-a343-ab266f554822/context 200 2500 ms - 76
[HTTP] --> GET /wd/hub/session/5edc2707-fe06-4e4a-a343-ab266f554822/context {}
[MJSONWP] Calling AppiumDriver.getCurrentContext() with args: [“5edc2707-fe06-4e4a-a343-ab266f554822”]
[MJSONWP] Responding to client with driver.getCurrentContext() result: “WEBVIEW_com.example.android.apis”
[HTTP] <-- GET /wd/hub/session/5edc2707-fe06-4e4a-a343-ab266f554822/context 200 6 ms - 106
[HTTP] --> POST /wd/hub/session/5edc2707-fe06-4e4a-a343-ab266f554822/element {“using”:“xpath”,“value”:”/html/body/a"}
[MJSONWP] Driver proxy active, passing request on via HTTP proxy
[JSONWP Proxy] Proxying [POST /wd/hub/session/5edc2707-fe06-4e4a-a343-ab266f554822/element] to [POST http://127.0.0.1:9515/wd/hub/session/690190e7735af36a2d524a0bbf89f15d/element] with body: {“using”:“xpath”,“value”:"/html/body/a"}
[JSONWP Proxy] Got response with status 200: {“sessionId”:“690190e7735af36a2d524a0bbf89f15d”,“status”:0,“value”:{“ELEMENT”:“0.8669703400228173-1”}}
[JSONWP Proxy] Replacing sessionId 690190e7735af36a2d524a0bbf89f15d with 5edc2707-fe06-4e4a-a343-ab266f554822
[HTTP] <-- POST /wd/hub/session/5edc2707-fe06-4e4a-a343-ab266f554822/element 200 195 ms - 106
[HTTP] --> GET /wd/hub/session/5edc2707-fe06-4e4a-a343-ab266f554822/element/0.8669703400228173-1/text {}
[MJSONWP] Driver proxy active, passing request on via HTTP proxy
[JSONWP Proxy] Proxying [GET /wd/hub/session/5edc2707-fe06-4e4a-a343-ab266f554822/element/0.8669703400228173-1/text] to [GET http://127.0.0.1:9515/wd/hub/session/690190e7735af36a2d524a0bbf89f15d/element/0.8669703400228173-1/text] with body: {}
[JSONWP Proxy] Got response with status 200: “{“sessionId”:“690190e7735af36a2d524a0bbf89f15d”,“status”:0,“value”:“Hello World! - 1”}”
[JSONWP Proxy] Replacing sessionId 690190e7735af36a2d524a0bbf89f15d with 5edc2707-fe06-4e4a-a343-ab266f554822
[HTTP] <-- GET /wd/hub/session/5edc2707-fe06-4e4a-a343-ab266f554822/element/0.8669703400228173-1/text 200 152 ms - 90
[HTTP] --> DELETE /wd/hub/session/5edc2707-fe06-4e4a-a343-ab266f554822 {}
[MJSONWP] Calling AppiumDriver.deleteSession() with args: [“5edc2707-fe06-4e4a-a343-ab266f554822”]
[debug] [AndroidDriver] Shutting down Android driver
[debug] [AndroidDriver] Stopping chromedriver for context WEBVIEW_com.example.android.apis

Ohhh Thanks priyank.

Please let me summarize, and correct me if i am wrong or missing anything.

In my application(APIDemo), the “setWebContentDebuging” was set to false, so the reason i am not able to interact with the controls.

If i make "setWebContentDebuging(true), then which will also work for me.

Yes… That is limitation of UIAutomator which appium uses under the hood. So you need to modify application’s code.

1 Like