# Cant get any element after changing context to webview

**URL:** <https://discuss.appium.io/t/cant-get-any-element-after-changing-context-to-webview/40437>\
**Category:** Support\
**Created:** [October 4, 2023, 6:20pm UTC](https://discuss.appium.io/t/cant-get-any-element-after-changing-context-to-webview/40437 "2023-10-04T18:20:30Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![Andrew\_Medvedev](https://discuss.appium.io/user_avatar/discuss.appium.io/andrew_medvedev/32/11750_2.png) [@Andrew\_Medvedev](https://discuss.appium.io/u/Andrew_Medvedev)\
**Post date:** [October 5, 2023, 2:49pm UTC](https://discuss.appium.io/t/cant-get-any-element-after-changing-context-to-webview/40437/41 "2023-10-05T14:49:51Z")

</div>

NATIVE\_APP  
WEBVIEW\_com.dkveuroservice.mobileappkit  
Found contexts size: ‘2’  
Found context: ‘NATIVE\_APP’  
Found context: ‘WEBVIEW\_com.dkveuroservice.mobileappkit’  
Switching context to: ‘WEBVIEW\_com.dkveuroservice.mobileappkit’  
Current Context: WEBVIEW\_com.dkveuroservice.mobileappkit  
bodyEl.size: 1

---

<div class="post-metadata">

**Author:** ![Aleksei](https://discuss.appium.io/user_avatar/discuss.appium.io/aleksei/32/6267_2.png) [@Aleksei](https://discuss.appium.io/u/Aleksei)\
**Post date:** [October 5, 2023, 2:51pm UTC](https://discuss.appium.io/t/cant-get-any-element-after-changing-context-to-webview/40437/42 "2023-10-05T14:51:59Z")

</div>

found. now try

```auto
               try {
                    Document doc = Jsoup.parseBodyFragment(driver.findElement(By.cssSelector("body")).getAttribute("outerHTML"));
                    System.out.println("\n" + doc.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }

```

i use

```auto
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.15.3</version>
            <scope>test</scope>
        </dependency>

```

---

<div class="post-metadata">

**Author:** ![Andrew\_Medvedev](https://discuss.appium.io/user_avatar/discuss.appium.io/andrew_medvedev/32/11750_2.png) [@Andrew\_Medvedev](https://discuss.appium.io/u/Andrew_Medvedev)\
**Post date:** [October 5, 2023, 2:57pm UTC](https://discuss.appium.io/t/cant-get-any-element-after-changing-context-to-webview/40437/43 "2023-10-05T14:57:40Z")

</div>

> [@Aleksei](#):
>
> try { Document doc = Jsoup.parseBodyFragment(driver.findElement(By.cssSelector(“body”)).getAttribute(“outerHTML”)); System.out.println(“\n” + doc.toString()); } catch (Exception e) { e.printStackTrace(); }

Found contexts size: ‘2’  
Found context: ‘NATIVE\_APP’  
Found context: ‘WEBVIEW\_com.dkveuroservice.mobileappkit’  
Switching context to: ‘WEBVIEW\_com.dkveuroservice.mobileappkit’  
Current Context: WEBVIEW\_com.dkveuroservice.mobileappkit  
java.lang.NullPointerException: Cannot invoke “String.length()” because “s” is null

---

<div class="post-metadata">

**Author:** ![Aleksei](https://discuss.appium.io/user_avatar/discuss.appium.io/aleksei/32/6267_2.png) [@Aleksei](https://discuss.appium.io/u/Aleksei)\
**Post date:** [October 5, 2023, 2:59pm UTC](https://discuss.appium.io/t/cant-get-any-element-after-changing-context-to-webview/40437/44 "2023-10-05T14:59:20Z")

</div>

> [@Andrew\_Medvedev](#):
>
> because “s” is null

something in your code. “s” is your contex. no “s” in code i wrote 🙂

---

<div class="post-metadata">

**Author:** ![Andrew\_Medvedev](https://discuss.appium.io/user_avatar/discuss.appium.io/andrew_medvedev/32/11750_2.png) [@Andrew\_Medvedev](https://discuss.appium.io/u/Andrew_Medvedev)\
**Post date:** [October 5, 2023, 3:20pm UTC](https://discuss.appium.io/t/cant-get-any-element-after-changing-context-to-webview/40437/45 "2023-10-05T15:20:11Z")

</div>

that is weird cause before i didn get it before and i didnt change anything in the code  
@Test  
@DisplayName(“DAF-T14 Check feedback form after clicking it from profile page”)  
public void ProfileAppFeedbackFlow() throws Exception {

```
    String expectedFeedbackTitle = "Feedback";
    String expectedFeedbackWebTitle = "What are your experiences with the DKV app?";

    var onboardingPage = new OnboardingPage();
    var onboardingLinearLayout = onboardingPage.clickSkipButton();
    var locationPopUp = onboardingLinearLayout.clickLaterButton();
    var analysisPopUp = locationPopUp.clickAllowAllTimeButton();
    analysisPopUp.clickActivateButton();
    var feedbackProfilePage = new DoYouLikePopUp().clickNoButton();

```

Set strcontext = null;  
try{  
strcontext = ((AndroidDriver) AppiumInit.getAppiumDriver()).getContextHandles();  
System.out.println(“Found contexts size: '” + strcontext.size() + “’”);  
for (String s :strcontext) {  
System.out.println(“Found context: '” + s + “’”);  
if (s != null && s.contains(“WEBVIEW”)) {  
System.out.println(“Switching context to: '” + s + “’”);  
((AndroidDriver) AppiumInit.getAppiumDriver()).context(s);  
break;  
}  
}  
} catch (Exception e){  
e.printStackTrace();  
}

String currentContext = ((AndroidDriver) AppiumInit.getAppiumDriver()).getContext();  
System.out.println("Current Context: " + currentContext);

```
    sleep(5000);
    try {
        Document doc = Jsoup.parseBodyFragment(((AndroidDriver) AppiumInit.getAppiumDriver()).findElement(By.cssSelector("body")).getAttribute("outerHTML"));
        System.out.println("\n" + doc.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }

```

}  
That is weird cause it happens when i add your code.

---

<div class="post-metadata">

**Author:** ![Aleksei](https://discuss.appium.io/user_avatar/discuss.appium.io/aleksei/32/6267_2.png) [@Aleksei](https://discuss.appium.io/u/Aleksei)\
**Post date:** [October 5, 2023, 3:25pm UTC](https://discuss.appium.io/t/cant-get-any-element-after-changing-context-to-webview/40437/46 "2023-10-05T15:25:28Z")

</div>

if you do not switch context. what is in ?

```auto
System.out.println(driver.getPageSource());

```

---

<div class="post-metadata">

**Author:** ![Andrew\_Medvedev](https://discuss.appium.io/user_avatar/discuss.appium.io/andrew_medvedev/32/11750_2.png) [@Andrew\_Medvedev](https://discuss.appium.io/u/Andrew_Medvedev)\
**Post date:** [October 5, 2023, 3:29pm UTC](https://discuss.appium.io/t/cant-get-any-element-after-changing-context-to-webview/40437/47 "2023-10-05T15:29:12Z")

</div>

> <https://gist.github.com/Nifargo/9c187b65de0188a3849f0fec01e96e9d>

---

<div class="post-metadata">

**Author:** ![Aleksei](https://discuss.appium.io/user_avatar/discuss.appium.io/aleksei/32/6267_2.png) [@Aleksei](https://discuss.appium.io/u/Aleksei)\
**Post date:** [October 5, 2023, 3:30pm UTC](https://discuss.appium.io/t/cant-get-any-element-after-changing-context-to-webview/40437/48 "2023-10-05T15:30:21Z")

</div>

1 so you have all visible. why not try interact here without switch to WebView?  
2 you app has few webviews or it is pure web? What you test?

---

<div class="post-metadata">

**Author:** ![Andrew\_Medvedev](https://discuss.appium.io/user_avatar/discuss.appium.io/andrew_medvedev/32/11750_2.png) [@Andrew\_Medvedev](https://discuss.appium.io/u/Andrew_Medvedev)\
**Post date:** [October 5, 2023, 3:41pm UTC](https://discuss.appium.io/t/cant-get-any-element-after-changing-context-to-webview/40437/49 "2023-10-05T15:41:05Z")

</div>

1. cause even without changing context i got error - org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters.

and this page is webview so that is why i thought that i need to change the context  
2. as far as i can see from the contexts - we have 1 NATIVE and 1 WEBVIEW. but if i dont use

```
    capabilities.setCapability("appPackage", "com.dkveuroservice.mobileappkit");
    capabilities.setCapability("appActivity", "com.dkveuroservice.mobileappkit.MainActivity");

```

i saw one more webview\_staging

---

<div class="post-metadata">

**Author:** ![Aleksei](https://discuss.appium.io/user_avatar/discuss.appium.io/aleksei/32/6267_2.png) [@Aleksei](https://discuss.appium.io/u/Aleksei)\
**Post date:** [October 5, 2023, 3:45pm UTC](https://discuss.appium.io/t/cant-get-any-element-after-changing-context-to-webview/40437/50 "2023-10-05T15:45:51Z")

</div>

1. what you need on this screen? Enter some value into edit input and tap Submit?
2. i did not get. How many webViews you have? 1,2,5, many or all ?

---

<div class="post-metadata">

**Author:** ![Andrew\_Medvedev](https://discuss.appium.io/user_avatar/discuss.appium.io/andrew_medvedev/32/11750_2.png) [@Andrew\_Medvedev](https://discuss.appium.io/u/Andrew_Medvedev)\
**Post date:** [October 5, 2023, 3:50pm UTC](https://discuss.appium.io/t/cant-get-any-element-after-changing-context-to-webview/40437/51 "2023-10-05T15:50:17Z")

</div>

1. 

2. We have only 1 webview right now

---

<div class="post-metadata">

**Author:** ![Aleksei](https://discuss.appium.io/user_avatar/discuss.appium.io/aleksei/32/6267_2.png) [@Aleksei](https://discuss.appium.io/u/Aleksei)\
**Post date:** [October 5, 2023, 3:55pm UTC](https://discuss.appium.io/t/cant-get-any-element-after-changing-context-to-webview/40437/52 "2023-10-05T15:55:54Z")

</div>

One web view? wow forget about switch. try simple:

```auto
        WebElement inputField = driver.findElement(AppiumBy.className("android.widget.EditText"));
        inputField.sendKeys("test feedback");
        WebElement submitButton = driver.findElement(AppiumBy.className("android.widget.Button"));
        tap(submitButton); // tap function - whatever you know how to tap.

```

---

<div class="post-metadata">

**Author:** ![Andrew\_Medvedev](https://discuss.appium.io/user_avatar/discuss.appium.io/andrew_medvedev/32/11750_2.png) [@Andrew\_Medvedev](https://discuss.appium.io/u/Andrew_Medvedev)\
**Post date:** [October 5, 2023, 3:58pm UTC](https://discuss.appium.io/t/cant-get-any-element-after-changing-context-to-webview/40437/53 "2023-10-05T15:58:15Z")

</div>

ooo didnt understand you - we have 5-7 webview pages

I thought that we are talking about webview context

But il try your way right now

---

<div class="post-metadata">

**Author:** ![Aleksei](https://discuss.appium.io/user_avatar/discuss.appium.io/aleksei/32/6267_2.png) [@Aleksei](https://discuss.appium.io/u/Aleksei)\
**Post date:** [October 5, 2023, 4:00pm UTC](https://discuss.appium.io/t/cant-get-any-element-after-changing-context-to-webview/40437/54 "2023-10-05T16:00:45Z")

</div>

In most cases it easier not to switch. Problem happens often when we have large and complex webView page or after we scroll it.

---

<div class="post-metadata">

**Author:** ![Andrew\_Medvedev](https://discuss.appium.io/user_avatar/discuss.appium.io/andrew_medvedev/32/11750_2.png) [@Andrew\_Medvedev](https://discuss.appium.io/u/Andrew_Medvedev)\
**Post date:** [October 6, 2023, 8:15am UTC](https://discuss.appium.io/t/cant-get-any-element-after-changing-context-to-webview/40437/55 "2023-10-06T08:15:00Z")

</div>

yes it works but how can i understand why I cant use webview for testing?

---

<div class="post-metadata">

**Author:** ![Aleksei](https://discuss.appium.io/user_avatar/discuss.appium.io/aleksei/32/6267_2.png) [@Aleksei](https://discuss.appium.io/u/Aleksei)\
**Post date:** [October 6, 2023, 8:22am UTC](https://discuss.appium.io/t/cant-get-any-element-after-changing-context-to-webview/40437/56 "2023-10-06T08:22:44Z")

</div>

> [@Andrew\_Medvedev](#):
>
> why I cant use webview for testing?

no answer 🙂 sometimes it hard to understand issue without having test app…

---

<div class="post-metadata">

**Author:** ![Andrew\_Medvedev](https://discuss.appium.io/user_avatar/discuss.appium.io/andrew_medvedev/32/11750_2.png) [@Andrew\_Medvedev](https://discuss.appium.io/u/Andrew_Medvedev)\
**Post date:** [October 9, 2023, 11:18am UTC](https://discuss.appium.io/t/cant-get-any-element-after-changing-context-to-webview/40437/57 "2023-10-09T11:18:44Z")

</div>

i thnk i understood why i can get any webview element

in devtools - I found one more page

 ![41](https://discuss.appium.io/uploads/default/original/2X/b/bbc4734b5a6fe2edcdb11ef6c5929a947ba16780.png)

So Feedback page is mine but I don’t know how did I get - about blank page

and it happens with all webview pages  
And did I understand correctly if I have 2 webviews pages - I have to have 2 webviews contexts?

---

<div class="post-metadata">

**Author:** ![Aleksei](https://discuss.appium.io/user_avatar/discuss.appium.io/aleksei/32/6267_2.png) [@Aleksei](https://discuss.appium.io/u/Aleksei)\
**Post date:** [October 9, 2023, 12:21pm UTC](https://discuss.appium.io/t/cant-get-any-element-after-changing-context-to-webview/40437/58 "2023-10-09T12:21:23Z")

</div>

Had same. In such case full webcontext name helps to understand where to switch.

Context size = 2 - \> switch to whatever webContext have. More then 2 need read context name and switch to correct.

Ps in one more difficult situation even wrote search element in all webContexs.

---

<div class="post-metadata">

**Author:** ![Andrew\_Medvedev](https://discuss.appium.io/user_avatar/discuss.appium.io/andrew_medvedev/32/11750_2.png) [@Andrew\_Medvedev](https://discuss.appium.io/u/Andrew_Medvedev)\
**Post date:** [October 10, 2023, 11:47am UTC](https://discuss.appium.io/t/cant-get-any-element-after-changing-context-to-webview/40437/59 "2023-10-10T11:47:50Z")

</div>

yes but even in this case i have only 1 webview context

NATIVE\_APP  
WEBVIEW\_com.dkveuroservice.mobileappkit.staging

so how can I use the correct webview page?

---

<div class="post-metadata">

**Author:** ![Aleksei](https://discuss.appium.io/user_avatar/discuss.appium.io/aleksei/32/6267_2.png) [@Aleksei](https://discuss.appium.io/u/Aleksei)\
**Post date:** [October 10, 2023, 12:19pm UTC](https://discuss.appium.io/t/cant-get-any-element-after-changing-context-to-webview/40437/60 "2023-10-10T12:19:07Z")

</div>

[Hybrid app has multiple WebViews showing in Chrome inspector; how do I switch between them?](https://discuss.appium.io/t/hybrid-app-has-multiple-webviews-showing-in-chrome-inspector-how-do-i-switch-between-them/6383/8) -\> `driver.getWindowHandles();`

PS Andrew start using forum search 🙂

[Previous page](https://discuss.appium.io/t/cant-get-any-element-after-changing-context-to-webview/40437.md?page=2)
