我正在尝试使用Selenium和WebDriver以及页面对象模式来打开表格中的链接。我无法控制网页代码。如何使用Selenium WebDriver和页面对象模式单击多个链接的TableCell中的特定链接?
另外请注意,这是一个简化的例子,但它的工作和显示的问题。
工作编码: https://gist.github.com/charlesgreen/b80ed7e164b7199eaa44229e104f4428
该表具有与在单个细胞中1个或2个链接的列。
对于只有1个链接的单元格,我可以通过调用单元格上的.link_element.click打开链接。
对于具有2个链接的单元格,这将打开单元格中的第一个链接,但我试图打开第二个链接(事实)。有没有办法单击单元格中的事实链接(即索引或迭代)?
注意: 原来的网站我正在与这两个链接打开,但我无法在本地重现问题有了这个说,这不是我的目标。我正在尝试打开第二个链接。下面的代码和上面的要点链接可以重现。
# 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>
IMO,同时为参考不慎id'的'独特'中'Facts – DebanjanB
迷路了伪HTML这是一个有点不寻常一个'click'调用会触发2个不同的链接。你能创建一个可重现的测试用例(即HTML和页面目标代码)吗?目前的例子似乎过于简化,因为我无法重现问题,并且会产生异常。 –
@DebanjanB谢谢你的提示。我修复了ID。 –