Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android espresso webview access element in iframe

This time I try to use espresso to test my app. I use webview to open the url and check below html code I face the question that I can`t access the element in iframe.

<html>
  <head></head>
  <body>
    <iframe id="mainFrame" src="Address/Default.aspx" scrolling="no" 
    style="border: 0px; width: 100%; height: 432px; position: absolute;">
      <html>
        <head></head>
        <body>
          <button onclick = "one function">
        </body>
      </html>
    </iframe>
  </body>
</html>

I copy the XPTH that is /html/body/button and code

onWebView().withElement(findElement(Locator.XPATH, "/html/body/button")).perform(webClick());

However, I get Atom evaluation returned null! and I can not get element. Can espresso get element inside iframe? I can get element not in iframe.

like image 988
呂裕翔 Avatar asked Jan 01 '26 08:01

呂裕翔


1 Answers

As the button is inside an iframe, thus we need to use .inWindow(selectFrameByIdOrName("mainFrame"))

onWebView()
    .inWindow(selectFrameByIdOrName("mainFrame"))
    .withElement(findElement(Locator.XPATH, "/html/body/button")).perform(webClick());

Hope that helps.

like image 65
Stack Overflowerrr Avatar answered Jan 02 '26 23:01

Stack Overflowerrr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!