title: Selenium Webdriver (Appium) - switching to a UIWebview in a Hybrid app (iOS)
body: <p>I am using a hybrid app and writing tests using Appium + Selenium Webdriver in Ruby.</p>
  
  <p>I start my test with some textbox editing + click a button to open the UIWebview (so far everything works). The problem is when the UIWebview is opened - I cannot access it (it is immediately closed when I'm trying to click a html element (I am using Appium inspector to find elements and to record my Ruby test). I understand that I have to switch to the UIWebview (as I found <a href="https://github.com/appium/appium/blob/master/docs/hybrid.md" rel="nofollow">here</a>), but I cannot make it to work. </p>
  
  <p>Code example:</p>
  
  <pre><code>require 'rubygems'
  require 'selenium-webdriver'
  capabilities = {
      'browserName' =&gt; 'iOS',
      'platform' =&gt; 'Mac',
      'version' =&gt; '7.1',
      'device' =&gt; 'iPhone Retina (4-inch)',
      'app' =&gt; '/Users/{my user here}/Library/Developer/Xcode/DerivedData/{path here}/Build/Products/Debug-iphonesimulator/SDK.app'
  }
  
  server_url = "http://127.0.0.1:4723/wd/hub"
  
  @wd = Selenium::WebDriver.for(:remote, :desired_capabilities =&gt; capabilities, :url =&gt; server_url)
  # ...
  # Do all kind of native actions here 
  # ...
  
  @wd.find_element(:name, "showWebviewButton").click
  @wd.manage.timeouts.implicit_wait = 30 # seconds
  # ???
  # How do I switch to my UIWebview here??? 
  # (cannot access html elements here with @wd.find_element(:name, "htmlElement"))
  # ???
  
  @wd.quit
  </code></pre>
  
  <p><strong>EDIT</strong>:
  Using Appium inspector I found that my UIWebview is "window(1) ", so I tried:</p>
  
  <pre><code>@wd.switch_to.window(1)
  </code></pre>
  
  <p>This gives me the error: </p>
  
  <pre><code>A request to switch to a different window could not be satisfied because the window could not be found
  </code></pre>
  
  <p>(The error is thrown before the UIWebview is loaded)</p>