簡體   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