【问题标题】:How to find correct horizontal and vertical offset of an element?如何找到元素的正确水平和垂直偏移量?
【发布时间】:2019-08-05 16:51:56
【问题描述】:

我目前正在使用 Selenium 和 Ruby。我的问题是将什么传递给 Selenium Web Driver 的 move_by 方法?

#move_by(right_by, down_by, device: nil) ⇒ W3CActionBuilder
driver.action.move_by(100, 100).perform

document 是这样说的:

参数:

  • right_by (Integer) — 水平偏移。负值表示移动 鼠标离开。
  • down_by (Integer) — 垂直偏移。负值表示移动 鼠标向上。
  • device (Symbol || String) — PointerInput 设备的可选名称 移动

所以我去使用 Chrom Dev 工具找到了元素的 DOMRect 并在控制台中运行了这个命令:

$0.getBoundingClientRect()

返回这些值:

bottom: 410
height: 14
left: 1801.65625
right: 1817.65625
top: 396
width: 16
x: 1801.65625
y: 396

那么,问题是上面的数字中我的 right_by 和 down_by 参数是什么?

【问题讨论】:

  • 那么在找到了要移动到的元素之后,您是否有某些原因使用 move_by() 而不是 move_to(element)
  • x = right_byy = down-by

标签: javascript css ruby selenium


【解决方案1】:

您可以使用 javascript 滚动到所需的位置。 Javascript:

window.scrollTo(100,100);

在 ruby​​ 中调用 Javascript:

driver.execute_script("window.scrollTo(1801.65625,396);")

如果您尝试滚动到某个元素,也可以使用scroll.to,以防万一。

driver.element().scroll.to :bottom 

【讨论】:

  • 那么我的 100、100 是多少?根据我提供的数字?
  • 用您问题中的 x,y 值更新了答案。如果您正在处理元素而不是浏览器本身,还请检查另一个选项以滚动到元素。
  • driver.element().scroll.to :bottom 这是 ruby​​ 语法吗?
  • 是的。 driver.element(xpath: "xpath goes here").scroll.to :bottom
  • 我收到一个错误:Minitest::UnexpectedError: NoMethodError: undefined method `element' for
猜你喜欢
  • 1970-01-01
  • 2014-03-05
  • 2013-10-27
  • 2015-12-13
  • 2018-04-01
  • 1970-01-01
  • 2022-09-23
  • 1970-01-01
相关资源
最近更新 更多