如何使用 Selenium WebDriver 和页面对象模式单击具有多个 link 的 TableCell 中的特定 link?

How to click on a specific link within a TableCell with multiple links using Selenium WebDriver and Page Object Pattern?

我正在尝试将 Selenium 与 WebDriver 和页面对象模式结合使用,以在 table 中打开 links。我无法控制网页代码。

另请注意,这是一个简化的示例,但它确实有效并显示了问题。

工作代码: https://gist.github.com/charlesgreen/b80ed7e164b7199eaa44229e104f4428

table 有一列在单个单元格中包含 1 或 2 个 link。

对于只有 1 个 link 的单元格,我可以通过在单元格上调用 .link_element.click 打开 link。

对于具有 2 个 link 的单元格,这将打开单元格中的第一个 link,但我正在尝试打开第二个 link(事实)。有没有办法只点击单元格中的事实 link(即索引或迭代)?

注意: 我正在使用两个 link 的原始网站都打开了,但是我无法在本地重现这个问题,这不是我的 objective。我正在尝试打开第二个 link。这可以在下面的代码中与上面的要点 link 一起重现。

# products.rb
require 'selenium-webdriver'
require './product_page'

Selenium::WebDriver.logger.output = 'selenium.log'
Selenium::WebDriver::Chrome.driver_path = '/Applications/chromedriver'
browser = Selenium::WebDriver.for :chrome

# UPDATE File path
browser.get('file:///Users/name/products/index.html')

product_page = ProductPage.new(browser)
product_page.open_facts_link


# product_page.rb
require 'page-object'

class ProductPage
    include PageObject
    table(:products, id: 'products')

    def open_facts_link
       products_element[2][0].link_element.click
    end
end


# index.html - validated with https://validator.nu/
<!DOCTYPE html>
<head>
    <title>Link Page</title>
    <script>
        function OpenPhotos(val) {
            var opened = window.open("");
            opened.document.write("<html><head><title>Photos</title></head><body>Photos</body></html>");
        }

        function OpenFacts(val) {
            var opened = window.open("");
            opened.document.write("<html><head><title>Facts</title></head><body>Facts</body></html>");
        }
    </script>
</head>
<body>
    <table id="products">
        <tr>
            <th>Links</th>
            <th>Description</th>
        </tr>
        <tr>
            <td>
                <ul>
                    <li>
                        <a id="A_35" href="javascript:OpenFacts('35')" target="_blank">Facts</a>
                    </li>
                </ul>
            </td>
            <td>Product 1</td>
        </tr>
        <tr>
            <td>
                <ul>
                    <li>
                        <a id="A_36" href="javascript:OpenPhotos('36')" target="_blank">Photos</a>
                    </li>
                    <li>
                        <a id="L_36" href="javascript:OpenFacts('36')" target="_blank">Facts</a>
                    </li>
                </ul>
            </td>
            <td>Product 2</td>
        </tr>
    </table>
</body>

要单击同一单元格中的不同链接,您可以将定位器传递给 link_element 以更加具体。在这种情况下,您可以使用 href 属性来区分 2 个链接:

# Click Facts link
products_element[2][0].link_element(href: /OpenFacts/).click

# Click Photos link
products_element[2][0].link_element(href: /OpenPhotos/).click