繁体   English   中英

如何使用Selenium-Python突出显示网页上的元素?

[英]How can I highlight element on a webpage using Selenium-Python?

我已经交出了一个现有的使用python编写脚本的硒框架。 为了调试(及其他目的),我想突出显示当前正在对其执行操作的元素(输入框,链接,下拉列表等)。

尽管我可以找到定义函数并在需要突出显示元素的地方调用函数的解决方案(如下例所示),但我需要的是框架级别的解决方案。

  1. Selenium Webdriver高亮元素,然后单击
  2. https://seleniumwithjavapython.wordpress.com/selenium-with-python/intermediate-topics/playing-with-javascript-and-javascript-executor/highlighting-a-web-element-on-webpage/

是否有可能在Python(或可以与python脚本集成的任何其他语言)的框架/脚本级别实现任何解决方案,因此我不必显式调用这些函数。

PS:我才刚刚开始使用Python,所以如果它简单/直接,请原谅。 如果有人可以指出我现有的解决方案或提供自己的解决方案,将不胜感激。

我没有尝试过这段代码,但是应该可以。

import time

def highlight(element):
 """Highlights (blinks) a Selenium Webdriver element"""
  driver = element._parent
  def apply_style(s):
     driver.execute_script("arguments[0].setAttribute('style', arguments[1]);",element, s)
  original_style = element.get_attribute('style')
  apply_style("background: yellow; border: 2px solid red;")
  time.sleep(.3)
  apply_style(original_style)

希望这可以帮助。 谢谢。

来源-https: //gist.github.com/dariodiaz/3104601

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM