I tried to add adblock to the selenium using this code:
chop =webdriver.ChromeOptions()
chop.add_extension('Adblock-Plus_v1.4.1.crx')
driver = webdriver.Chrome(chrome_options=chop)
I got this error:
'OSError: Path to the extension doesn't exist'
what should I do?
You need to provide the full path:
from os import path
chop =webdriver.ChromeOptions()
chop.add_extension(path.abspath('Adblock-Plus_v1.4.1.crx'))
driver = webdriver.Chrome(chrome_options=chop)
Related
Problem:
I just want to open a URL with selenium in python but it didn't work.
This is my current code:
from selenium import webdriver
path='C:\Program Files (x86)\chromdriver.exe'
url='https://anywebpage.de'
browser = webdriver.Chrome(path)
Technical specification:
Windows 10 Pro 64 bit Python Version 3.9
I get this error message:
"Message: 'chromdriver.exe' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home"
I cannot find a solution out in the documentation
Has anyone else an idea?
The exe.file is in the correct path stored.
from selenium import webdriver
PATH = "C:\Chrome Webdriver\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("Your-website-here")
If you are using windows, press shift and right click on chromedriver to copy it's path Image
"C:\Chrome Webdriver\chromedriver.exe" was my own personal path, you must copy yours.
Also, in your code above, your file path is missing an "e" in chromedriver
When I am passing fully qualified path for the executable_path the Chrome() method able to locate the driver but when I am creating a directory(drivers) inside the project in pycharm and passing as
driver = webdriver.Chrome(executable_path="../drivers/chromedriver")
then Chrome() method fails to locate.
According to me, error reason would be:
1) chromedriver copied into the local directory is not supporting.(Getting a question mark on the copied chromedriver executable file)
2) Proper path is not passed
Image Of My Structure & Error Image
Try This:
pip install webdriver_manager
Then:
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
driver = webdriver.Chrome(ChromeDriverManager().install())
When using a relative path, it needs to include the r to specify it's raw. Also, include the .exe in the path for the driver.
For example,
from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'../drivers/chromedriver.exe')#Use single quotation mark
Apply the same changes to yours and it should solve your problem.
Try To add one dot
Try This:
from selenium import webdriver
driver = webdriver.Chrome(executable_path="./driver/chromedriver")
dealing with paths can be a bit tricky, i always use a lib
import pathlib
from selenium import webdriver
driver = webdriver.Chrome(executable_path=str(pathlib.Path().absolute().parent.joinpath('drivers').joinpath('chromedriver'))))
hope it helps.
if __name__ == '__main__':
driver=webdriver.Firefox(executable_path=r'/home/saurabh/Saurabh/LearnPython/Automation/geckodriver');
After running the above code I am getting an error as :
selenium.common.exceptions.WebDriverException: Message: Unable to find a matching set of capabilities
I don't see any significant error in your code as such.
It is to be noted that the current Selenium-Python binding is unstable with geckodriver and looks to be Architecture specific. You can find the github discussion and merge here. So you may additionally need to pass the absolute path of the firefox binary as firefox_binary argument while initializing the webdriver
Here is your own code with a simple tweak which opens the Mozilla Firefox browser:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
if __name__ == '__main__':
binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary, executable_path="C:\\path\\to\\geckodriver.exe")
Make sur you are pointing to \path\to\FirefoxPortable\App\Firefox64\firefox.exe and not just \path\to\FirefoxPortable\FirefoxPortable.exe
After running pip selenium and downloading chromedriver.exe to C:/chromedriver/chromedriver.exe directory. Running my program results in Chrome not being able to open the URL and the following message prompts in a dialog-box:
Chromedriver stopped working
This is my attempt at testing that the source-page could be accessed.
import requests
from selenium import webdriver
Base_url = "https:/www.facebook.com"
driver = webdriver.Chrome(r'C:/chromedriver/chromedriver.exe')
driver.get(Base_url)
print (driver.page_source)
Can someone help me sort this out?
Thank you.
To open Chrome Browser using chromedriver binary you need to pass the argument executable_path along with the absolute path of the chromedriver binary and invoke the proper url as follows :
from selenium import webdriver
Base_url = "https://www.facebook.com/"
driver = webdriver.Chrome(executable_path=r'C:/chromedriver/chromedriver.exe')
driver.get(Base_url)
print (driver.page_source)
I am trying to load a google chrome extension with Selenium WebDriver.
But I receive an error OSError: Path to the extension doesn't exist.
Here is the code I am using:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
from os import path
path = "path to chrome driver"
chrome_options = webdriver.ChromeOptions()
chrome_options.add_extension('Adblock-Plus_v1.12.4_0.crx') # ALTERNATIVE 0
driver = webdriver.Chrome(path, chrome_options=chrome_options)
After reading various similar questions on this site I tried the following two alternatives:
# Alternative 1
chrome_options.add_extension('~/Library/Application\ Support/Google/Chrome/Default/Extensions/[Extension ID]/Adblock-Plus_v1.12.4_0.crx')
#Alternative 2
chrome_options.add_extension(path.abspath("Adblock-Plus_v1.12.4_0.crx"))
But none of them work. Alternative 1 gives same error message as the original code whereas Alternative 2 gives error AttributeError: 'str' object has no attribute 'abspath'
Does anyone have a clue what I could be doing differently?
More thank likely, this is because python is referencing the wrong path with what would normally be the home directory shortcut of ~/ in the path. Python will try and run the file from the current directory, so for example if your code is in ~/Dev/testproject, and the code being called above is really trying to run /home/username/Dev/testproject/~/Library/Application Support/Google/Chrome/Default/Extensions/[Extension ID]/Adblock-Plus_v1.12.4_0.crx
Try using the following:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
import os
chromedriver = "path to chrome driver"
chrome_options = webdriver.ChromeOptions()
# choose one of the following 2:
chrome_options.add_extension(os.path.expanduser('~/Library/Application\ Support/Google/Chrome/Default/Extensions/[Extension ID]/Adblock-Plus_v1.12.4_0.crx')) # Option 1: if your extension is not also in your project folder
chrome_options.add_extension(os.path.abspath('Adblock-Plus_v1.12.4_0.crx')) # Option 2: if your extension IS in your project folder
driver = webdriver.Chrome(chromedriver, chrome_options=chrome_options)
EDIT: avoid declaring a variable named path since you are importing path from os. This is why you are getting the error on Alternative #2.