Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

javascript - How can I highlight element on a webpage using Selenium-Python?

I have been handed over a existing selenium framework which uses python for scripting. For debugging(& other) purposes, I would like to highlight the element on which action is being taken currently (input box, link, drop-down etc.)

Though I could find solutions to define a function and calling the function wherever I need to highlight the element(as examples given below), what I need is a solution at the framework level.

  1. Selenium webdriver highlight element before clicking
  2. https://seleniumwithjavapython.wordpress.com/selenium-with-python/intermediate-topics/playing-with-javascript-and-javascript-executor/highlighting-a-web-element-on-webpage/

Is it possible to implement any solution at the framework / script level in Python (or any other language which can be integrated with python scripts), so that I don't have to call the functions explicitly.

P.S. I am just beginning to use Python, so excuse me if its a simple/straight forward. Will appreciate if someone can point me to any existing solution or can provide their own solution.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I haven't tried this code, but this should work.

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)

Hope this helps. Thanks.

Source - https://gist.github.com/dariodiaz/3104601


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...