[英]javascript error: circular reference error while trying to retrieve navigator.plugins using Selenium and Python
我正在嘗試從Selenium驅動的ChromeDriver啟動的 google-chrome Browsing Context中檢索navigator.plugins
的值。
使用google-chrome-devtools我可以檢索navigator.userAgent
和navigator.plugins
如下:
但是使用Selenium的execute_script()
方法我能夠提取navigator.userAgent
但navigator.plugins
引發以下循環引用錯誤:
代碼塊:
from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument("start-maximized") driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe') driver.get("https://www.google.com/") print("userAgent: "+driver.execute_script("return navigator.userAgent;")) print("plugins: "+driver.execute_script("return navigator.plugins;"))
控制台 Output:
userAgent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36 Traceback (most recent call last): File "C:\Users\Soma Bhattacharjee\Desktop\Debanjan\PyPrograms\navigator_properties.py", line 19, in <module> print("vendor: "+driver.execute_script("return navigator.plugins;")) File "C:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 636, in execute_script 'args': converted_args})['value'] File "C:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response) File "C:\Python\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.JavascriptException: Message: javascript error: circular reference (Session info: chrome=83.0.4103.116)
我已經通過以下關於循環引用的討論,我理解了這個概念。 但我不確定我應該如何在這里解決這個問題。
有人可以幫我找回navigator.plugins
嗎?
我假設它與navigator.plugins
返回一個PluginArray的事實有關。
PluginArray
頁面列出了可用的方法和屬性,並且我編寫了返回名稱列表的代碼。 您可以根據需要對其進行調整。
print("plugins: " + driver.execute_script("var list = [];for(var i = 0; i < navigator.plugins.length; i++) { list.push(navigator.plugins[i].name); }; return list.join();"))
如果兩個單獨的對象相互傳遞引用,就會發生循環引用。 循環引用意味着相互引用的 2 個對象是緊密耦合的,對一個 object 的更改可能也需要對另一個進行更改。
NavigatorPlugins.plugins返回一個PluginArray object,列出了描述應用程序中安裝的插件的插件對象。 plugins
是PluginArray
object 用於通過名稱或作為項目列表訪問Plugin
對象。 返回的值具有長度屬性並支持使用括號表示法(例如plugins[2]
)以及通過item(index)
和namedItem("name")
方法訪問單個項目。
要提取navigator.plugins
屬性,您可以使用以下解決方案:
要獲取plugins
名稱列表:
print(driver.execute_script("return Array.from(navigator.plugins).map(({name}) => name);"))
控制台 Output:
['Chrome PDF Plugin', 'Chrome PDF Viewer', 'Native Client']
要獲取plugins
的文件名列表:
print(driver.execute_script("return Array.from(navigator.plugins).map(({filename}) => filename);"))
控制台 Output:
['internal-pdf-viewer', 'mhjfbmdgcfjbbpaeojofohoefgiehjai', 'internal-nacl-plugin']
要獲取plugins
的描述列表:
print(driver.execute_script("return Array.from(navigator.plugins).map(({description}) => description);"))
控制台 Output:
['Portable Document Format', '', '']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.