簡體   English   中英

模塊“selenium.webdriver”沒有屬性“firefoxprofile”

[英]module 'selenium.webdriver' has no attribute 'firefoxprofile'

我正在使用 selenium 進行 web 抓取。我遇到了一個問題。

Python version 3.9.7, window 10

代碼:

url=['https://www.tradingview.com/markets/stocks-usa/market-movers-large-cap/']

catergories=['Overview','Xperformance','Valuation','Dividends','Margins','Income Statement','Balance Sheet','Oscillator','Trend-Following']

user_agent= 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:87.0)Gecko/20100101 Firefox/87.0'

FireFoxDriverPath = os.path.join(os.getcwd(),'Drivers','Geckodriver.exe')

FireFoxProfile = webdriver.FirefoxProfile()

FireFoxProfile.set_preference("general.useragent.override",user_agent)

browser = webdriver.Firefox(executable_path=FireFoxDriverPath)

browser.implicitly_wait(7)

url= 'https://www.tradingview.com/markets/stocks-usa/market-movers-large-cap/'

browser.get(url)

錯誤:

module 'selenium.webdriver' has no attribute 'firefoxprofile'

這些是我在更改 FirefoxProfile() 消息后遇到的新錯誤:“Geckodriver.exe”可執行文件需要位於 PATH 中。

能夠使用FirefoxProfile

你可以做

from selenium.webdriver import FirefoxProfile
profile = FirefoxProfile()

set_preference如下所示:

profile.set_preferences("general.useragent.override", user_agent)

這個錯誤信息...

module 'selenium.webdriver' has no attribute 'firefoxprofile'

...暗示selenium.webdriver沒有firefoxprofile這樣的屬性

而不是firefoxprofile()它應該是FirefoxProfile() 實際上,您的代碼行應該是:

基於的代碼塊

from selenium import webdriver

user_agent= 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:87.0)Gecko/20100101 Firefox/87.0'
FireFoxDriverPath = os.path.join(os.getcwd(),'Drivers','Geckodriver.exe')
firefoxprofile = webdriver.FirefoxProfile()
firefoxprofile.set_preferences("general.useragent.override", user_agent)

browser = webdriver.Firefox(firefox_profile=firefoxprofile, executable_path=FireFoxDriverPath)

您可以在webdriver.FirefoxProfile() 中找到相關討論:Is it possible to use a profile without makes a copy of it?


更新

使用 FirefoxProfile()已被棄用,使用使用自定義配置文件你必須使用Options的實例。

可以在DeprecationWarning找到相關討論:firefox_profile has been deprecated, please pass in an Options object

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM