Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 在Watir WebDriver中查找文本项旁边的图像图标_Ruby_Xpath_Watir Webdriver - Fatal编程技术网

Ruby 在Watir WebDriver中查找文本项旁边的图像图标

Ruby 在Watir WebDriver中查找文本项旁边的图像图标,ruby,xpath,watir-webdriver,Ruby,Xpath,Watir Webdriver,上下文是我正在使用watir webdriver,我需要定位图像是否出现在列表中某个特定项之前 更具体地说,网站的某个部分有文章上传给他们。这些文章出现在一个列表中。结构如下所示: <div id="article-resources" <ul class="components"> ... <li> <div class="component"> <img src="some/path/articl

上下文是我正在使用watir webdriver,我需要定位图像是否出现在列表中某个特定项之前

更具体地说,网站的某个部分有文章上传给他们。这些文章出现在一个列表中。结构如下所示:

<div id="article-resources"
  <ul class="components">
    ...
    <li>
      <div class="component">
        <img src="some/path/article.png">
        <div class="replies">
          <label>Replies</label>
        </div>

        <div class="subject">
          <a href="/article">Saving the Day</a>
        </div>
      </div>
    </li>
    ...
  </ul>
</div>
不过,这对我没有好处,因为关键的鉴别器似乎是li[2],但我不能指望这篇文章总是排在第二位

所以我试了一下:

article_image = '//div[@class="component"]/a[contains(.,"Saving the Day")]/../img'
@browser.image(:xpath => article_image).exist?.should be_true
我得到的结果是:

expected: true value
got: false (RSpec::Expectations::ExpectationNotMetError)
所以它没有找到图像,这可能意味着我做错了什么,因为我确定测试在正确的页面上

我的想法是,我可以使用上面的方法来获取div区域中被称为类“component”的任何link(a)标记。检查链接是否有文本,然后“备份”一级以查看是否有图像

我甚至没有检查确切的图像,我可能应该这样做。我只是看看有没有图像

所以我想我的问题是:

  • 我的XPath有什么问题
  • 这是解决这个问题的最好办法吗

    • 使用水

      有几种可能的方法

      一种方法是找到链接,转到组件div,然后检查图像:

      browser.link(:text => 'Saving the Day').parent.parent.image.present?
      

      另一种对更改更具鲁棒性的方法是查找包含链接的组件div:

      browser.divs(:class => 'component').find { |component|
        component.div(:class => 'subject', :text => 'Saving the Day').exists?
      }.image.present?
      
      使用XPath

      当然,上述操作也可以通过xpath完成

      以下是您更正的xpath:

      article_image = '//div[@class="component"]//a[contains(.,"Saving the Day")]/../../img'
      puts browser.image(:xpath => article_image).present?
      
      或者:

      article_image = '//a[contains(.,"Saving the Day")]/../../img'
      browser.image(:xpath => article_image).present?
      
      同样,还有自上而下的方法:

      article_image = '//div[@class="component"][//a[contains(.,"Saving the Day")]]/img'
      browser.image(:xpath => article_image).present?
      

      您可以在本书中阅读更多关于这些方法和其他选项的信息。

      这非常有用。对你的答复,我深表感谢。我正在调查你提到的Watirways参考资料。谢谢你的帮助。
      article_image = '//a[contains(.,"Saving the Day")]/../../img'
      browser.image(:xpath => article_image).present?
      
      article_image = '//div[@class="component"][//a[contains(.,"Saving the Day")]]/img'
      browser.image(:xpath => article_image).present?