Python Selenium-高亮元素没有任何作用 [英] Python Selenium - highlight element doesn't do anything

查看:163
本文介绍了Python Selenium-高亮元素没有任何作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python硒突出显示以下网页上的元素.我正在使用此处发布的解决方案:如何可以使用Selenium-Python突出显示网页上的元素吗?,但是根本没有任何效果.我没有收到任何错误消息,它只是没有突出显示我选择的元素. 有人遇到过同样的问题吗? 这是我的代码:

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)

推荐答案

对我来说很好.请注意,它仅在 0.3 秒内突出显示元素(添加 2像素红色边框),所以您可能会错过这种效果

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

您可以向功能添加更多参数,例如TimeToHighlight,Color,BorderSize:

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)

然后以

open_window_elem = driver.find_element_by_id("openwindow")
highlight(open_window_elem, 3, "blue", 5)

这将向元素添加蓝色5像素边框,持续 3

This will add blue 5 pixels border to element for 3 seconds

这篇关于Python Selenium-高亮元素没有任何作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆