我正在嘗試使用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。 –