How to install Chrome Extension using Selenium & Python - python

Hello I'm trying to install a Chrome extension with Selenium using python, I tried using ChromeDriver - WebDriver for Chrome
But it is not working, this is my code:
from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import ChromeOptions
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.expected_conditions import presence_of_element_located
import re # regular expressions, are imported from python directly
import time
import numpy as np
import pandas as pd
import functions_database
# Pandas read CSV
df_read = pd.read_csv(
'/home/daniel/amazon-project-scrapers/ss_scraper.edited2.csv')
amazon_data = list(df_read.amz_search)
# Chrome Driver + install plugin
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/home/daniel/amazon-project-scrapers/chromedriver_linux64/DS-Amazon-Quick-View_v2.8.9.crx"));
ChromeDriver driver = new ChromeDriver(options);
driver = webdriver.Chrome(executable_path='/home/daniel/amazon-project-scrapers/chromedriver_linux64/chromedriver')
driver.get('https://www.amazon.com/')
And this is the error i'm getting:
File "camel_scraper.py", line 23
ChromeOptions options = new ChromeOptions();
^
SyntaxError: invalid syntax
I tried to do this in other 3 different ways, actually there is a similar question in Stack overflow whose answer is deprecated, if I find it again I'll write the link in here.

To add/install the DS-Amazon-Quick-View Chrome extension using Selenium's python client you can use the following splution:
Code Block:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_extension('/home/daniel/amazon-project-scrapers/chromedriver_linux64/DS-Amazon-Quick-View_v2.8.9.crx')
driver = webdriver.Chrome(options=chrome_options, executable_path='/path/to/chromedriver')
driver.get('https://www.google.co.in')
Reference
You can find a couple of relevant discussions in:
[Python] How to load extension within chrome driver in selenium with python
[Java] How to install extension permanently in geckodriver

Related

InvalidArgumentException: Message: binary is not a Firefox executable error in Python linuxmint environment

I test selenium python and I have these errors with firefox and I can't find the solution. Here my code
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
options = Options()
options.binary = FirefoxBinary(r'/usr/bin/firefox')
driver = webdriver.Firefox(executable_path= r'./scrap/geckodriver', options=options)
driver.get("http://www.google.com")
print (driver.page_source.encode('utf-8') )
driver.close()
My error message python linuxmint :
selenium.common.exceptions.InvalidArgumentException: Message: binary is not a Firefox executable
I've been searching the forum for a few days, but I can't find a solution for linux mint.
This is an example of working selenium/Firefox setup on Debian, which should also work on Mint, given it's essentially the same os:
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options as Firefox_Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
import time as t
import pandas as pd
firefox_options = Firefox_Options()
# firefox_options.add_argument("--width=1280")
# firefox_options.add_argument("--height=720")
driverService = Service('chromedriver/geckodriver') ## path where you saved geckodriver
browser = webdriver.Firefox(service=driverService, options=firefox_options)
Selenium docs: https://www.selenium.dev/documentation/
As an alternative to options.binary = FirefoxBinary() you can use the binary_location option as follows:
options = Options()
options.binary_location = r'/usr/bin/firefox'

python selenium sendkey doesn't work in chrome

I'm trying to launch some browser (Chrome) functions by sending shortcuts
I have tried several methods but they all cannot work.
I do this by following these steps
Initialize the browser
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
options = Options()
options.add_argument("--user-data-dir="+r"path_to_user_data")
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())
,options=options
)
Through actionchain
ActionChains(driver).key_down(Keys.CONTROL).key_down('T').key_up('T').key_up(Keys.CONTROL).perform
Through send_key
driver.find_element(by=By.XPATH,value="/html/body").send_keys(Keys.CONTROL+"T")
But it doesn't work. This is confused for me. Why it cannot work?
It is possible to open a new tab and switch to it with a single command
driver.switch_to.new_window()
To Open a blank new tab you can use the below:
driver.execute_script("window.open('');")
Switch to the new window
driver.switch_to.window(driver.window_handles[1])

How to specify the folder when downloading a file with slelenium python

Here's my code :
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
PATH = "driver\chromedriver.exe"
options = webdriver.ChromeOptions()
p = {"download.default_directory": "C:\\Users", "safebrowsing.enabled":"false"}
options.add_experimental_option("prefs", p)
driver = webdriver.Chrome(options=options, executable_path=PATH)
url = 'https://www.mergermarket.com/homepage'
driver.get(url)
download = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="__next"]/div/div/div[1]/div/div[2]/div[1]/div/div/div[2]/button[1]')))
download.click()
I made some research but almost everyone recommende to use download.default_directory with ChromeOptions. I did like you can see above but it didn't work. Well, the code work but my file is downloaded in download and not the path I specified in my code.
You can copy automatically downloaded file in author path with os or shutill librarie in python

Python Selenium Error: PlacementList must be sorted by first 8 bits of display_id

I try to use Python and Selenium, but I get this error: PlacementList must be sorted by first 8 bits of display_id
How can I solve this?
This is the code:
from selenium import webdriver
e = "C:/Downloads/edgedriver_win64/msedgedriver.exe"
driver = webdriver.Edge(e)
driver.get("http://www.python.org")
Try this if it helps
from selenium import webdriver
from webdriver_manager.microsoft import EdgeChromiumDriverManager
driver = webdriver.Edge(EdgeChromiumDriverManager().install())
driver.get("http://www.python.org")
If you want to use chrome as a browser try the following code.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromiumService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.utils import ChromeType
browser = webdriver.Chrome(service=ChromiumService(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()))
browser.get('www.your-website.com')
However, I advise using Firefox which is the more stable one as follow.
from selenium import webdriver
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))
# Open the Website
driver.get('https://www.your-website.com')
print(driver.title)
driver.quit()

Python, selenium, Chrome and ActionChains to save a webpage

I am trying to have Python, Chrome and ActionChains to save a webpage.
The code seemed like ever works fine on an computer but not another.
I checked the code and also tried the suggestions gave at selenium action chains no effect, but it still doesn't work.
Where went wrong? thanks.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import win32com.client as comclt
chromedriver = "c:\Python27\\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
driver.maximize_window()
aaa = driver.get("https://sg.yahoo.com/?p=us")
time.sleep(3)
save_me = ActionChains(driver).key_down(Keys.CONTROL).key_down('s').key_up(Keys.CONTROL).key_up('s')
save_me.perform()
time.sleep(2)
wsh= comclt.Dispatch("WScript.Shell")
wsh.AppActivate("chrome")
time.sleep(1)
wsh.SendKeys("{ENTER}")
driver.quit()
it seems like the versions for Chrome and Chromedriver (32-bit, 64-bit) matter. Tried another version it may work fine.

Categories