获取元素的位置

时间:2022-06-01 16:19:38

I use watir-webdriver and try to find the position of an element like $browser.div(:id, "foo"). When i try to click on it, the chromedriver says

我使用watir-webdriver并尝试找到一个元素的位置,比如$browser。div(:id、“foo”)。当我试图点击它时,chromedriver说

Element is not clickable at point(-1707, -799.5)

元素在点(-1707,-799.5)不能点击

How is it possible to get this coordinates of the element? I tried this:

怎么可能得到这个元素的坐标呢?我试着这样的:

http://wiki.openqa.org/display/WTR/Right+Click+an+Element

http://wiki.openqa.org/display/WTR/Right +单击+ +元素

But I get an error when I tried to call the document() method.

但是当我尝试调用document()方法时,会出现一个错误。

2 个解决方案

#1


8  

I assume you are using Watir-Webdriver since you are using Chromedriver. The article you reference is actually for Watir, which is not always the same as Watir-Webdriver.

我假设您使用的是Watir-Webdriver,因为您使用的是Chromedriver。您引用的文章实际上是针对Watir的,它并不总是与Watir- webdriver相同。

To get an element's location, you can use the Element#location method:

要获取元素的位置,可以使用元素#位置方法:

location = browser.element(:id, 'id').location
puts "location x = #{location.x}"
puts "location y = #{location.y}"

Note that older versions of Watir-Webdriver do not have this method. Instead, you'll need to directly access the underlying Selenium::WebDriver:

注意,旧版本的Watir-Webdriver没有这种方法。相反,您需要直接访问底层的Selenium::WebDriver:

location = browser.element(:id, 'id').wd.location
puts "location x = #{location[0]}"
puts "location y = #{location[1]}"

#2


1  

i think that location (.wd.location) is a relative location which is within the browser page range, not the absolute location of the system screen.

我认为位置(.wd.location)是在浏览器页面范围内的相对位置,而不是系统屏幕的绝对位置。

say, it gets (700, 300) as your browser is maximized. but it gets (500,200) as your browser is moved....

比方说,当你的浏览器最大化时,它会得到(700,300)。但它(500200)移动浏览器....

i got a button's "wd.location" and then sent a mouse-click event from Autoit"autoit.MouseClick("left", x, y)". I found the mouse is out of the button. I must add an offset to fix it, but real problem is that offset changes with my browser location...

我有一个按钮。然后从Autoit发送一个鼠标点击事件。MouseClick(“左”,x,y)”。我发现鼠标掉在按钮外面了。我必须添加一个偏移量来修复它,但真正的问题是偏移量的变化与我的浏览器位置……

#1


8  

I assume you are using Watir-Webdriver since you are using Chromedriver. The article you reference is actually for Watir, which is not always the same as Watir-Webdriver.

我假设您使用的是Watir-Webdriver,因为您使用的是Chromedriver。您引用的文章实际上是针对Watir的,它并不总是与Watir- webdriver相同。

To get an element's location, you can use the Element#location method:

要获取元素的位置,可以使用元素#位置方法:

location = browser.element(:id, 'id').location
puts "location x = #{location.x}"
puts "location y = #{location.y}"

Note that older versions of Watir-Webdriver do not have this method. Instead, you'll need to directly access the underlying Selenium::WebDriver:

注意,旧版本的Watir-Webdriver没有这种方法。相反,您需要直接访问底层的Selenium::WebDriver:

location = browser.element(:id, 'id').wd.location
puts "location x = #{location[0]}"
puts "location y = #{location[1]}"

#2


1  

i think that location (.wd.location) is a relative location which is within the browser page range, not the absolute location of the system screen.

我认为位置(.wd.location)是在浏览器页面范围内的相对位置,而不是系统屏幕的绝对位置。

say, it gets (700, 300) as your browser is maximized. but it gets (500,200) as your browser is moved....

比方说,当你的浏览器最大化时,它会得到(700,300)。但它(500200)移动浏览器....

i got a button's "wd.location" and then sent a mouse-click event from Autoit"autoit.MouseClick("left", x, y)". I found the mouse is out of the button. I must add an offset to fix it, but real problem is that offset changes with my browser location...

我有一个按钮。然后从Autoit发送一个鼠标点击事件。MouseClick(“左”,x,y)”。我发现鼠标掉在按钮外面了。我必须添加一个偏移量来修复它,但真正的问题是偏移量的变化与我的浏览器位置……