【问题标题】:Clicking a link within nested tables using page-object使用页面对象单击嵌套表中的链接
【发布时间】:2014-07-11 22:50:26
【问题描述】:

我有一个嵌套在表格层中的链接,并且大部分表格没有任何 ID。我拥有的链接位于包含 id 的表下。无论如何我都无法点击链接。这是html。请帮忙。谢谢!

<html
 <frame src="my_frame"
   <html
    <table style="height:28px;"
     <table style="height:28px;"
      .
      .
       <table style="height:28px;"
         <table id="tblButtons"
           <td id="" title="" onmouseover="this.className = 'hdrBtn hdrBtnover';" onmouseout="this.className = 'hdrBtn';" onmousedown="this.className = 'hdrBtn hdrBtndown';" onmouseup="this.className = 'hdrBtn hdrBtnover';" background="../images/header/barbkg.gif" onclick="if(pageIsReady())parent.ordersummaryform.SubmitIt('OK');" class="hdrBtn"><a href="#" id="" class="linkForTabbingOnly" style="color:009900">Desktop</a></td>
             <a href="#" id="" class="linkForTabbingOnly" style="color:009900">Desktop</a>

我尝试过如下使用 xpath:

link(  :desktop_header, :xpath => "a[@href='#' and @class='linkForTabbingOnly' and starts-with(text(),'Desktop')]")

link(  :desktop_header, :xpath => "//table[@id='tblButtons']//a[@href='#' and @class='linkForTabbingOnly' and starts-with(text(),'Desktop')]")

我也试过

我试过了

table(:table_button) {frame_element(:src =&gt; 'quotesummary_buttons.cfm?QuoteFormat=').table_element(:id =&gt; 'tblButtons',:href =&gt; '#', :class =&gt; 'linkForTabbingOnly' ) }

def click_desktop() table_button_element.cell_element(:class => 'hdrBtn') .child .link_element(:text => 'Desktop') .click end

并使用 click_desktop 单击链接,但出现错误 NoMethodError:#

的未定义方法“frame_element”

【问题讨论】:

    标签: watir-webdriver page-object-gem


    【解决方案1】:

    根据您的部分 HTML 和尝试,我猜问题的症结在于框架:

    1. 前几次尝试没有考虑框架。与其他元素不同,您必须明确告诉 Watir 何时查看帧。
    2. 在后一个示例中,考虑了框架。然而,正如例外所说,使用frame_element 来定义框架是不存在的。

    Page-Object gem wiki page 很好地解释了如何使用框架。

    提供的 HTML 似乎有些零散,因此您可能需要对以下建议进行一些调整。但是,希望通过 wiki 页面信息和一些更具体的示例,您可以解决您的问题。

    第一步是确保链接访问器被告知查看框架:

    class MyPage
      include PageObject
    
      in_frame(:src => 'my_frame') do |frame|
        link(:desktop_header, :href => '#', :class => 'linkForTabbingOnly', :text => /^Desktop/, :frame => frame)  
      end
    end
    

    除非框架中有多个带有文本“桌面”的链接,否则我可能会建议将链接定位器简化为仅文本 - 即 href 和 class 属性似乎没有添加任何内容。

    class MyPage
      include PageObject
    
      in_frame(:src => 'my_frame') do |frame|
        link(:desktop_header, :text => 'Desktop', :frame => frame)
      end
    end
    

    如果页面上有多个“桌面”链接,但“tblButtons”表中只有一个这样的链接,则可以使用嵌套。在下面的示例中,使用 :frame 定位器将表定位在框架范围内。然后使用块和嵌套元素方法将链接定位在表中。

    class MyPage
      include PageObject
    
      in_frame(:src => 'my_frame') do |frame|
        table(:table_buttons, :id => 'tblButtons', :frame => frame)
        link(:desktop_header) { table_buttons_element.link_element(:text => 'Desktop') }
      end
    end
    

    虽然上述示例的概念应该有效,但如果提供的部分 HTML 内容,它们很有可能会失败。您可能需要进行一些更改:

    1. 这些示例假定框架的 :src 属性是“my_frame”,这就是问题的 HTML 示例中显示的内容。但是,该问题的一次尝试表明 :src 实际上是“quotesummary_buttons.cfm?QuoteFormat="。因此,您应该确保in_frame(:src =&gt; 'my_frame') 中正确的:src(或其他定位器)是正确的。
    2. html 元素中有一个frame 元素似乎很不寻常。如果我没记错的话,框架只允许在frameset 元素中使用。您实际上可能正在处理iframe 而不是frame。如果是这样,您需要将in_frame 方法更改为in_iframe。请注意,无论哪种方式,对于框架中的元素,定位器仍然只是 :frame

    【讨论】:

    • 谢谢贾斯汀。它确实是一个框架,而不是 iframe。抱歉回复晚了。由于环境关闭,我无法测试。
    【解决方案2】:

    一般来说,您应该尽量避免使用 xpath。它非常脆弱,您的代码可能很快就会崩溃!

    尝试嵌套以下元素:

    @browser.table(:id, /Buttons/).link(:class, /link/)
    

    我认为阅读defining different elements. 的演练会让您受益匪浅,这很有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 2015-09-23
      • 1970-01-01
      • 1970-01-01
      • 2017-05-07
      • 2019-12-24
      • 1970-01-01
      相关资源
      最近更新 更多