I'm trying to highlight elements on the following webpage using python selenium. I'm using the solution posted here: How can I highlight element on a webpage using Selenium-Python? but it doesn't produce any effect at all. I don't get any error message, it simply doesn't highlight the element I've selected. Has anybody faced the same problem? Here is my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
chromeOptions = webdriver.ChromeOptions()
driver = webdriver.Chrome()
driver.maximize_window()
url = "https://learn.letskodeit.com/p/practice"
driver.get(url)
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("border: 2px solid red;")
time.sleep(.3)
apply_style(original_style)
open_window_elem = driver.find_element_by_id("openwindow")
highlight(open_window_elem)
Works fine for me. Note that it highlights element (add 2 pixels red border) for 0.3 seconds only, so you might just miss that effect
You can add more parameters to function, like TimeToHighlight, Color, BorderSize:
def highlight(element, effect_time, color, border):
"""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("border: {0}px solid {1};".format(border, color))
time.sleep(effect_time)
apply_style(original_style)
and then call as
open_window_elem = driver.find_element_by_id("openwindow")
highlight(open_window_elem, 3, "blue", 5)
This will add blue 5 pixels border to element for 3 seconds