Selenium webdriver 在单击之前突出显示元素

Selenium webdriver highlight element before clicking(Selenium webdriver 在单击之前突出显示元素)

本文介绍了Selenium webdriver 在单击之前突出显示元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一组用 Python 编写的 selenium UI 测试.在以前的项目中,我使用的是 WatiN 和 C#.

I'm working on a set of selenium UI tests that are written in Python. On a previous project I was using WatiN and C#.

WatiN 框架有一个您可以启用的功能,它可以突出显示"您正在单击、选择或输入的元素.执行操作时,元素周围会出现黄色边框.我发现这在对损坏的测试进行故障排除时非常有用,如果单击了错误的元素,我经常可以从测试运行中看到.

The WatiN framework had a feature you could enable that would 'highlight' the element you were clicking, selecting or typing into. The element would get a yellow border around it while the action was performed. I found this extremely helpful while troubleshooting broken tests, I could often see from the test run if the wrong element was being clicked.

我想知道 Selenium webdriver 是否有类似的功能可以打开.基本上我想要某种视觉指示正在与什么元素进行交互.

I was wondering if Selenium webdriver has a similar feature that I can turn on. Basically I would like some sort of visual indication of what element is being interacted with.

谢谢!

推荐答案

Selenium RC 可以做到这一点,但在 WebDriver 中没有直接的 API 方法可以做到这一点.您的选择是:

Selenium RC could do this but there is no direct API method to do this in WebDriver. Your options are:

1) 在您的语言绑定中使用 WebDriverBackedSelenium 实现来访问 Highlight 方法.

1) Use the WebDriverBackedSelenium implementation in your language bindings to access the Highlight method.

2) 只需使用 JavascriptExecutor(或您的语言绑定中的类似实现)调用一些 javascript 来为您完成工作.这将模仿 Selenium RC/选项 1 将实现的功能.

2) Simply call some javascript using the JavascriptExecutor (or similar implementation in your language bindings) to do the job for you. This will mimic what Selenium RC/option 1 would achieve.

要调用的 Javascript 存在于此处:

The Javascript to call exists here:

http://code.google.com/p/selenium/source/browse/javascript/selenium-core/scripts/htmlutils.js

要注意的函数是 highlight 函数.只需使用 JavascriptExecutor 加载此脚本(搜索以了解如何在您的编程语言中执行此操作).

The function to note is the highlight function. Just load this script using the JavascriptExecutor (search to find out how to do this in your programming language).

然后您可以比较它以了解 Java API 中的 WebDriverBackedSelenium 实现如何使用它:

You can then compare this to see how the WebDriverBackedSelenium implementation in the Java API's are using it:

http://code.google.com/p/selenium/source/browse/java/client/src/org/openqa/selenium/internal/seleniumemulation/Highlight.java

使用这个,在你自己的代码中做同样的事情.

Use this, to do this same in your own code.

附带说明一下,用于 Firefox 的 Selenium IDE 也可以突出显示方法.

As a side note, the Selenium IDE for Firefox can highlight methods as well.

这篇关于Selenium webdriver 在单击之前突出显示元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Selenium webdriver 在单击之前突出显示元素

基础教程推荐