簡體   English   中英

如何在watir中查找頁面對象以訪問div內的iframe

[英]How to find page object to access iframe inside div in watir

I want to find a page-object for an Iframe that is inside div element.

<html>
Iframe inside the div element
<div name=first>
    <iframe> 
    </iframe>
</div>
<div name=second>
    <iframe>
    </iframe>
</div>

我試圖找到 div_element 並開始訪問 iframe,但我得到了未定義的方法。

任何人都可以幫助我們識別和訪問 div 內 iframe 的頁面對象嗎?

頁面對象 gem 沒有用於框架/iframe 的嵌套元素方法。 您需要找到一個定位器,該定位器可以相對於瀏覽器(或另一個框架/iframe)唯一地標識框架/iframe。 唯一支持與其他元素相對位置的定位器是:css:xpath

請注意,頁面對象 gem 不保存對 frame/iframe 對象的引用(即,沒有像 Watir 中那樣的嵌套元素調用)。 相反,幀作為元素定位器的一部分傳遞。

例如,讓我們考慮以下 HTML,它添加(為簡潔起見內聯)一些框架內容:

<html>
  <body>
    <div name="first">
      <iframe src="frame1.htm">
        <html>
          <body>
            <span>frame1 - span</span>
            <input type="text" name="field" value="1" />
          </body>
        </html>
      </iframe>
    </div>
    <div name="second">
      <iframe src="frame2.htm">
        <html>
          <body>
            <span>frame2 - span</span>
            <input type="text" name="field" value="2" />
          </body>
        </html>
      </iframe>
    </div>
  </body>
</html>

根據您正在交互的內容,您可以使用訪問器或定義方法訪問 iframe 內容。 以下頁面對象使用訪問器方法。 請記住將框架定位器傳遞給每個元素。

class MyPage
  include PageObject

  # Define elements <div name="first"> iframe
  in_iframe(css: 'div[name="first"] iframe') do |f|
    span(:first_iframe_span, frame: f)
    text_field(:first_iframe_field, frame: f)
  end

  # Define elements <div name="second"> iframe
  in_iframe(css: 'div[name="second"] iframe') do |f|
    span(:second_iframe_span, frame: f)
    text_field(:second_iframe_field, frame: f)
  end
end

您可以看到每個定義的元素都返回來自特定 iframe 的值:

# Elements in the <div name="first"> iframe
p page.first_iframe_span
#=> "frame1 - span"
p page.first_iframe_field
#=> "1"

# Elements in the <div name="second"> iframe
p page.second_iframe_span
#=> "frame2 - span"
p page.second_iframe_field
#=> "2"

您可以使用類似的方法動態訪問方法中的元素:

class MyPage
  include PageObject

  def do_stuff_in_first_iframe
    in_iframe(css: 'div[name="first"] iframe') do |f|
      p span_element(frame: f).text
      p text_field_element(frame: f).value
    end
  end

  def do_stuff_in_second_iframe
    in_iframe(css: 'div[name="second"] iframe') do |f|
      p span_element(frame: f).text
      p text_field_element(frame: f).value
    end
  end
end

再次,您可以看到您獲得每個 iframe 的值:

page.do_stuff_in_first_iframe
#=> "frame1 - span"
#=> "1"

page.do_stuff_in_second_iframe
#=> "frame2 - span"
#=> "2"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM