[英]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() 。 實際上,您的代碼行應該是:
基於selenium3的代碼塊
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?
使用selenium4 FirefoxProfile()
已被棄用,使用selenium4使用自定義配置文件你必須使用Options
的實例。
可以在DeprecationWarning找到相關討論:firefox_profile has been deprecated, please pass in an Options object
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.