【问题标题】:Ruby- Selenium: Could we find an element by its 2 properties in parallel at the same time?Ruby- Selenium:我们可以同时通过它的 2 个属性找到一个元素吗?
【发布时间】:2013-07-23 13:24:27
【问题描述】:

首先,我们有一个这样的元素:

"房屋名称

我们知道,当在 ruby​​ 中通过 selenium 根据属性查找元素时,我们使用以下方法:

@driver.find_element(:id, "select_a_boundary") 或 @driver.find_element(class,"dataset_select2")

有人知道如何通过 selenium ruby​​ 中的属性 id 和 class 来找到这个元素吗?因为有时会有其他元素具有相同的 id 属性或相同的 class 属性。 并且假设我们不允许使用 Xpath 来组合它,我们该如何处理呢?谢谢大家的建议。

【问题讨论】:

  • 为什么你认为你不能使用xpath?你可以使用它。它内置于web-driver

标签: html ruby selenium selenium-webdriver


【解决方案1】:
     <div id="select_a_boundary" class="dataset_select2">Homes name</div>

硒代码:

@driver.find_element(:xpath, "//div[@id = 'select_a_boundary' and @class = 'dataset_select2']") 
@driver.find_element(:css, "div[id=select_a_boundary][class=dataset_select2]")
@driver.find_element(:css, "#select_a_boundary.dataset_select2")

【讨论】:

  • 感谢您的回答,但正如我上面所说,如果我们不允许使用 Xpath,我们如何找到它?
  • 当我尝试 @driver.find_element(:id=>"select_a_boundary", :class=>"dataset_select2") 它有效,但它似乎只关心第一个定位器。当第一个定位器为真时,它将通过,尽管第二个定位器是错误的。当第一个定位器错误时,它将失败。比如@driver.find_element(:id=>"select_a_boundary", :abcd=>"just test"),就会返回pass
  • @Popo 是的.. 不要使用它.. 如果你有问题.. 使用xpath,你不应该.. 使用CSS selectors.. 这些是很好的工具情况..
  • 因为我的作业要求不喜欢使用 xpath 和 css,所以我必须找到其他方法来做到这一点。感谢您的帮助。
  • @Popo 对不起,我不知道任何其他技术.. :((
【解决方案2】:

正如您所提到的,您不能使用 Xpath/CSS 尝试收集所有类似的搜索以列出,然后您可以使用收集的列表项来确定您的搜索。

【讨论】:

    猜你喜欢
    • 2014-01-01
    • 2014-04-04
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 2020-10-30
    • 1970-01-01
    • 2018-07-28
    相关资源
    最近更新 更多