Nat*_*atz 22 selenium selenium-rc
作为登录功能的硒测试的一部分,我想通过识别其坐标点击按钮,并指示硒点击这些坐标.这将在没有实际识别元素本身的情况下完成(通过id,xpath等).
我知道还有其他更有效的方法来运行click命令,但我希望专门使用这种方法来最好地匹配用户体验.谢谢.
Dir*_*rom 39
这里是一个办法做到这一点.使用ActionChains API,您可以将鼠标移到元素上,按一些偏移量调整(相对于元素的中间),然后单击该位置.以下是在Python中使用webdriver的方法:
elem = find_element_by_selector(selector)
ac = ActionChains(browser)
ac.move_to_element(elem).move_by_offset(x_off, y_off).click().perform()
Run Code Online (Sandbox Code Playgroud)
你们都要迅速解雇这个问题.有许多理由需要在特定位置点击,而不是在元素上点击.在我的例子中,我有一个带有覆盖元素的SVG条形图,可以捕获所有点击.我想在其中一个条上模拟点击,但由于叠加层在那里,Selenium无法点击元素本身.这种技术对于图像映射也很有价值.
cra*_*tad 17
在C#API中,您可以使用操作
var element = driver.FindElement(By...);
new Actions(driver).moveToElement(element).moveByOffset(dx, dy).click().perform();
Run Code Online (Sandbox Code Playgroud)
虽然最好尽可能使用简单的Id,CSS,Xpath选择器.但是在需要时功能就在那里(即在某些地理位置点击元素以获得功能).
Nar*_*raR 10
这可以使用Actionsclass in 来完成java
使用以下代码 -
new Actions(driver).moveByOffset(x coordinate, y coordinate).click().build().perform();
Run Code Online (Sandbox Code Playgroud)
注意: Selenium 3不支持Actions类geckodriver
另外,请注意x和y坐标是当前鼠标位置的相对值.假设鼠标坐标在(0,0)处开始,如果要使用绝对值,则可以在使用上面的代码单击后立即执行以下操作.
new Actions(driver).moveByOffset(-x coordinate, -y coordinate).perform();
我首先使用了JavaScript代码,在网站没有点击之前,它的运行效果惊人。
所以我找到了这个解决方案:
首先,为Python导入ActionChains并激活它:
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
Run Code Online (Sandbox Code Playgroud)
要单击会话中的特定点,请使用以下命令:
actions.move_by_offset(X coordinates, Y coordinates).click().perform()
Run Code Online (Sandbox Code Playgroud)
注意:上面的代码仅在未触摸鼠标的情况下有效,要重置鼠标坐标,请使用以下命令:
actions.move_to_element_with_offset(driver.find_element_by_tag_name('body'), 0,0))
Run Code Online (Sandbox Code Playgroud)
在全:
actions.move_to_element_with_offset(driver.find_element_by_tag_name('body'), 0,0)
actions.move_by_offset(X coordinates, Y coordinates).click().perform()
Run Code Online (Sandbox Code Playgroud)
如果您可以选择使用Selenium的商业插件,这是可能的:假设您的按钮位于坐标处x=123, y=456。然后您可以使用Helium在这些坐标处单击元素,如下所示:
from helium.api import *
# Tell Helium about your WebDriver instance:
set_driver(driver)
click(Point(123, 456))
Run Code Online (Sandbox Code Playgroud)
(我是 Helium 的作者之一。)
小智 5
这对我在 Java 中单击坐标(无论任何元素)都有效。
Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.tagName("body")), 0, 0);
actions.moveByOffset(xCoordinate, yCoordinate).click().build().perform();
Run Code Online (Sandbox Code Playgroud)
第二行代码将把光标重置到浏览器视图的左上角,最后一行将单击作为参数提供的 x,y 坐标。
| 归档时间: |
|
| 查看次数: |
71558 次 |
| 最近记录: |