I need to launch Firefox with Browsec add-on. I've written some code, but get an error:
from selenium import webdriver
url = input("Enter url to scrape from: ")
profile = webdriver.FirefoxProfile()
profile.add_extension('/home/myusername/.mozilla/firefox/b5qyukpg.default/extensions/browsec#browsec.com.xpi')
browser = webdriver.Firefox()
browser.get(url)
What i get is:
Traceback (most recent call last):
File "scraper.py", line 7, in <module>
profile.add_extension('/home/myusername/.mozilla/firefox/b5qyukpg.default/extensions/browsec#browsec.com.xpi')
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/firefox_profile.py", line 95, in add_extension
self._install_extension(extension)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/firefox_profile.py", line 274, in _install_extension
addon_details = self._addon_details(addon)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/firefox_profile.py", line 351, in _addon_details
raise AddonFormatError(str(e), sys.exc_info()[2])
selenium.webdriver.firefox.firefox_profile.AddonFormatError: ("[Errno 2] No such file or directory: '/tmp/tmpd7nyxubj.browsec#browsec.com.xpi/install.rdf'", <traceback object at 0x7f23f786eb08>)
How can i fix that error?
Related
I made an executable file of a python program I made.
It imports selenium , chromedriver , requests and os modules.
When I run the .exe file after entering the required inputs in the console, the website my program points to doesn't not opens.And the console closes immediately
from selenium import webdriver
from time import sleep
import requests
import os
class YoutubeBot():
def __init__(self,url_list,path):
self.driver = webdriver.Chrome(executable_path ="F:/Environments/project1_env/lib/site-packages/chromedriver/chromedriver.exe")
self.url_list= url_list
self.path = path
def download(self):
print("Starting Download.")
sleep(2)
c = 0
for self.url in self.url_list:
self.driver.get("https://en.savefrom.net/15/")
sleep(6)
self.driver.find_element_by_xpath('//*[#id="sf_url"]').send_keys(self.url)
self.driver.find_element_by_xpath('//*[#id="sf_submit"]').click()
sleep(15)
#self.driver.find_element_by_xpath('//*[#id="sf_result"]/div/div[1]/div[2]/div[2]/div[2]').click()
#elf.driver.find_element_by_xpath('//*[#id="sf_result"]/div/div[1]/div[2]/div[2]/div[2]/div[2]/div/div/div[1]/a[1]').click()
self.driver.find_element_by_xpath('//*[#id="sf_result"]/div/div[1]/div[2]/div[2]/div[1]/a').click()
#exit()
if c<5:
self.driver.switch_to.window(self.driver.window_handles[2])
sleep(2)
self.driver.close()
sleep(2)
self.driver.switch_to.window(self.driver.window_handles[0])
c =c +1
def startfile(self):
print('Opening File')
sleep(2)
os.startfile('{}'.format(self.path))
url_list = input().split(" ")
path = input("Path of the folder where the files have to be downloaded. \n")
my_bot = YoutubeBot(url_list,path)
my_bot.download()
my_bot.startfile()
I used cx_Freeze to convert it into a .exe file. Code for it is below:
from cx_Freeze import setup , Executable
setup(name = 'YoutubeBot',
version ='0.2',
description = 'Bot',
executables = [Executable("F:\projects\\YoutubeBot\\youtube_bot.py")])
error:
Traceback (most recent call last):
File "F:\Environments\project1_env\lib\site-packages\selenium\webdriver\common\service.py", line 72, in start
File "c:\users\hp\appdata\local\programs\python\python38-32\lib\subprocess.py", line 854, in __init__
File "c:\users\hp\appdata\local\programs\python\python38-32\lib\subprocess.py", line 1307, in _execute_child
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "F:\Environments\project1_env\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 40, in run
File "F:\Environments\project1_env\lib\site-packages\cx_Freeze\initscripts\Console.py", line 37, in run
File "F:\projects\YoutubeBot\youtube_bot.py", line 48, in <module>
File "F:\projects\YoutubeBot\youtube_bot.py", line 9, in __init__
File "F:\Environments\project1_env\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
File "F:\Environments\project1_env\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
selenium.common.exceptions.WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
I'm automating one of my regular processes by making a tool which uses Selenium. When I run the script in pyCharm, it works. But after converting it to an executable, it is throwing the error code below.
I looked at and tried some solutions available on the internet but I still can't get it resolved.
I have made two .py files, one main file calls another .py file. I enter the info into the GUI and when I press the submit button, it calls the 2nd file which has the code for downloading the required ChromeDriver version. When the line "ChromeDriverManager().install()" executes, it throws the below error.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
options = Options()
download_dir = r"C:\Users\{}\Documents\Downloaded_Report\\".format(user)
prefs = {'download.default_directory': download_dir, "directory_upgrade": True}
options.add_experimental_option("prefs", prefs)
options.add_argument("--start-maximized")
driver = webdriver.Chrome(ChromeDriverManager().install(),
chrome_options=options)
Error:
Exception in Tkinter callback
Traceback (most recent call last):
File "tkinter\__init__.py", line 1705, in __call__
File "AutoSpeedScript.py", line 28, in send_info_to_download_report
File "openPBMSite.py", line 197, in get_info
File "site-packages\webdriver_manager\chrome.py", line 15, in __init__
File "site-packages\webdriver_manager\driver.py", line 45, in __init__
File "site-packages\webdriver_manager\driver.py", line 18, in __init__
File "site-packages\webdriver_manager\config.py", line 28, in __getattr__
File "site-packages\webdriver_manager\config.py", line 21, in get
File "configparser.py", line 780, in get
File "configparser.py", line 1146, in _unify_values
configparser.NoSectionError: No section: 'ChromeDriver'
I am trying to make a function that refreshes any page when given the URL. But the program won't run. What am I doing wrong? This is my code:
from selenium import webdriver
import time
def page_refresh(url):
driver = webdriver.Firefox()
driver.get(url)
x = 0
while x <= 5:
time.localtime(10)
driver.refresh(url)
driver.close
page_refresh('https://www.wikipedia.org/')
This is what I get:
Traceback (most recent call last):
File "C:\Users\100453649\PycharmProjects\AutoRefresher\venv\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
stdin=PIPE)
File "C:\Users\100453649\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:\Users\100453649\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/100453649/PycharmProjects/AutoRefresher/Main.py", line 13, in <module>
page_refresh('https://www.wikipedia.org/')
File "C:/Users/100453649/PycharmProjects/AutoRefresher/Main.py", line 5, in page_refresh
driver = webdriver.Firefox()
File "C:\Users\100453649\PycharmProjects\AutoRefresher\venv\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 152, in __init__
self.service.start()
File "C:\Users\100453649\PycharmProjects\AutoRefresher\venv\lib\site-packages\selenium\webdriver\common\service.py", line 83, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
Your Exception clearly says that
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
Solution:
Add gecko driver path:
Example:
from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'your\path\geckodriver.exe')
driver.get(url)
I want to use selenium to scrape off some website. I can't access the website via my own internet connection, so I need to use browsec mozilla addon for that.
I am unable to launch firefox with selenium with the add-on enabled.
Here is what I have tried:
import selenium
from selenium import webdriver
url = "http://url"
profile = webdriver.FirefoxProfile()
profile.add_extension('browsec#browsec.com.xpi')
#profile.add_extension("C:\Users\urs\AppData\Roaming\Mozilla\Firefox\Profiles\abc.default\extensions\browsec#browsec.com.xpi")
driver = webdriver.Firefox(firefox_profile=profile)
if __name__ == "__main__":
driver.get(url)
driver.wait(5)
driver.quit()
I have tried putting the extension in the same directory where my script is and using the following
profile.add_extension('browsec#browsec.com.xpi')
which gives me this error when I run:
Traceback (most recent call last): File
"C:\Python36\lib\site-packages\selenium\webdriver\firefox\firefox_profile
.py", line 346, in _addon_details
with open(os.path.join(addon_path, 'install.rdf'), 'r') as f: FileNotFoundError: [Errno 2] No such file or directory:
'C:\Users\Usr\AppD
ata\Local\Temp\tmp0hny31u3.browsec#browsec.com.xpi\install.rdf'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "test.py", line 7, in
profile.add_extension("browsec#browsec.com.xpi") File "C:\Python36\lib\site-packages\selenium\webdriver\firefox\firefox_profile
.py", line 95, in add_extension
self._install_extension(extension) File "C:\Python36\lib\site-packages\selenium\webdriver\firefox\firefox_profile
.py", line 274, in _install_extension
addon_details = self._addon_details(addon) File "C:\Python36\lib\site-packages\selenium\webdriver\firefox\firefox_profile
.py", line 351, in _addon_details
raise AddonFormatError(str(e), sys.exc_info()[2]) selenium.webdriver.firefox.firefox_profile.AddonFormatError: ("[Errno
2] No such file or directory:
'C:\\Users\\Usr\\AppData\\Local\\Temp\\tmp0hn
y31u3.browsec#browsec.com.xpi\\install.rdf'", )
I also tried giving the path to the extension:
profile.add_extension("C:\Users\urs\AppData\Roaming\Mozilla\Firefox\Profiles\abc.default\extensions\browsec#browsec.com.xpi")
And I ran into this error:
profile.add_extension("C:\Users\Hassan\AppData\Roaming\Mozilla\Firefox\Profi
les\n5jwlj9l.default\extensions\browsec#browsec.com.xpi")
^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in positio n 2-3: truncated
\UXXXXXXXX escape
Formatting the path string like below doesn't help either.
profile.add_extension(r"C:\Users\urs\AppData\Roaming\Mozilla\Firefox\Profiles\abc.default\extensions\browsec#browsec.com.xpi")
I get the following:
Traceback (most recent call last): File "test.py", line 7, in
profile.add_extension(r"C:\Users\Hassan\AppData\Roaming\Mozilla\Firefox\Prof
iles\n5jwlj9l.default\extensions\browsec#browsec.com.xpi") File
"C:\Python36\lib\site-packages\selenium\webdriver\firefox\firefox_profile
.py", line 95, in add_extension
self._install_extension(extension) File "C:\Python36\lib\site-packages\selenium\webdriver\firefox\firefox_profile
.py", line 274, in _install_extension
addon_details = self._addon_details(addon) File "C:\Python36\lib\site-packages\selenium\webdriver\firefox\firefox_profile
.py", line 351, in _addon_details
raise AddonFormatError(str(e), sys.exc_info()[2]) selenium.webdriver.firefox.firefox_profile.AddonFormatError: ("[Errno
2] No such file or directory:
'C:\\Users\\usr\\AppData\\Local\\Temp\\tmp1he
0fym_.browsec#browsec.com.xpi\\install.rdf'", )
How do I configure selenium to run firefox with browsec enabled by default?
I found this article rather helpful.
Instead of adding the extension to the profile, you install it after the browser has been created:
from selenium import webdriver
driver = webdriver.Firefox()
# This installs adblock plus
driver.install_addon("/home/your_username/coding/Project/seleniumTest/adblock.xpi", temporary=True)
driver.get('https://www.stackoverflow.com')
Be sure to add the .xpi to your project folder!
You can try to create profile on firefox browser like - On windows Run --> type
"firefox.exe -P"
It will open profile manager. Create new profile. Start firefox from that profile, add plugins. And use that same profile with code..Sometime it worked for me..
Sorry for my English))
Most likely you are using the new version of Firefox (Quantum - from the 57th version inclusive). In newer versions of Firefox, the extension metadata is not stored in the install.rdf file, but in the manifest.json file. Selenium does not know this yet (in version 3.11, and learns only in 3.14). Therefore, when trying to connect an extension, it looks for habit install.rdf.
Here the author wrote a class that slightly changes the connection function of the extension, and instead of install.rdf, selenium looks for metadata in manifest.json.
What you need to do:
# Add Import
import json
import os
import sys
from selenium.webdriver.firefox.firefox_profile import AddonFormatError
# Add class
class FirefoxProfileWithWebExtensionSupport(webdriver.FirefoxProfile):
def _addon_details(self, addon_path):
try:
return super()._addon_details(addon_path)
except AddonFormatError:
try:
with open(os.path.join(addon_path, 'manifest.json'), 'r') as f:
manifest = json.load(f)
return {
'id': manifest['applications']['gecko']['id'],
'version': manifest['version'],
'name': manifest['name'],
'unpack': False,
}
except (IOError, KeyError) as e:
raise AddonFormatError(str(e), sys.exc_info()[2])
# Declare Firefox_profile written class
profile = FirefoxProfileWithWebExtensionSupport()
Further as usual)))
Good luck)))
I am using python 3.5, firefox 45 (also tried 49) and selenium 3.0.1
I tried:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
Then I got the error message:
C:\Users\A\AppData\Local\Programs\Python\Python35\python.exe
C:/Users/A/Desktop/car/test.py
Traceback (most recent call last):
File "C:\Users\A\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 64, in start
stdout=self.log_file, stderr=self.log_file)
File "C:\Users\A\AppData\Local\Programs\Python\Python35\lib\subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "C:\Users\A\AppData\Local\Programs\Python\Python35\lib\subprocess.py", line 1220, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/A/Desktop/car/test.py", line 4, in <module>
driver = webdriver.Firefox()
File "C:\Users\A\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 135, in __init__
self.service.start()
File "C:\Users\A\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 71, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x0000000000EB8278>>
Traceback (most recent call last):
File "C:\Users\A\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 163, in __del__
self.stop()
File "C:\Users\A\AppData\Local\Programs\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 135, in stop
if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
What can I do? Any help is much appreciated!
If you are using firefox ver >47.0.1 you need to have the [geckodriver][1] executable in your system path. For earlier versions you want to turn marionette off. You can to so like this:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
capabilities = DesiredCapabilities.FIREFOX.copy()
capabilities['marionette'] = False
driver = webdriver.Firefox(capabilities=capabilities)