使用 alt 定位器查找 img 并单击

Finding img and click using alt locator

我正在尝试使用 alt 定位在亚马逊书店上查找并点击一张图片,但只有一个单词(在本例中为单词 "Game")。

我尝试了什么:

 describe 'test' do

it 'visit Amazon' do

    visit 'https://www.amazon.com/s/browse?_encoding=UTF8&node=283155&ref_=nav_shopall-export_nav_mw_sbd_intl_books'

        expect(page.find('#image')['alt']).to match(/Game/)

        puts 'ok'


end

end

网站上的元素:

<img onload="window.uet &amp;&amp; uet.call &amp;&amp; uet(&quot;cf&quot;);" src="https://images-na.ssl-images-amazon.com/images/I/81VqkhMFpuL._AC_SR201,266_.jpg" alt="A Game of Thrones: A Song of Ice and Fire, Book 1" class="aok-align-center">

感谢您的帮助!

要通过其 alt 属性的部分内容查找 img 元素,您可以使用 CSS 属性选择器 lik

page.find('img[alt*="Game"]').click # alt attribute contains Game
page.find('img[alt~="Game"]').click # alt attribute contains the word Game

另一种选择,假设 img 被 link 扭曲,将使用 click_link 将(默认情况下部分)匹配包装图像的 :alt 属性这一事实 - https://www.rubydoc.info/github/teamcapybara/capybara/Capybara/Node/Actions#click_link-instance_method

page.click_link('Game')