configure RefControl extension using selenium webdriver - python

i am using this addon with selenium and it starts fine, but how do you go about affecting the settings of addons in selenium?
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox import webdriver
profile = FirefoxProfile('/path/to/profile')
driver = webdriver.WebDriver(firefox_profile = profile)
driver.get("http://localhost/referer.html")
driver.find_element_by_link_text("go there").click()
the problem is that this addon does not do anything unless you configure it first, but there is no documentation, that i can find on this.

General way, as alecxe suggested, "to install and configure the extension first, then use that firefox profile with selenium". This will probably work with any extension.
I also search for configuration in prefs.js (or about:conf) and found out, that it stores the value in "refcontrol.actions" preference. This way you will be able to change the extension behavior in your code. Example:
profile = FirefoxProfile('/path/to/profile')
profile.set_preference("refcontrol.actions", '#DEFAULT=#NORMAL example.com=#3RDPARTY:http://www.referer.com/your/referer');

Related

Selenium Python Edge disable `open office files in browser` setting

Python: 3.9.9
Selenium: 4.1.5
Edge: 101.0.1210.39 (X64) driver link
I am trying to automate downloading excel file from a website, but due to Edge's default setting of open office files in browser set to True, on pressing download button with selenium it redirects to Edge file viewer instead of downloading it.
Since I want to automate the process I don't want to manually go to settings and disable it every time.
Any work arounds will be appreciated too...
Thank you!
This is what worked for me:
from pathlib import Path
from selenium import webdriver
if Path('..\msedgedriver.exe').exists():
driver = webdriver.Edge('..\msedgedriver.exe')
# Settings
driver.get('edge://settings/downloads')
toggle = driver.execute_script('''
return document.querySelector(' input[aria-label="Open Office files in
the browser"]');
''')
toggle.click()
# continue...
similarly you can change any setting as per required.

Default download directory Edge web driver - python

I'm trying to change the download location of my web driver for Microsoft Edge but it does not seem to work.
I've tried looking at option for chrome and replicating it for Edge and this is what I got so far
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from msedge.selenium_tools import Edge, EdgeOptions
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
import time
from datetime import datetime
PATH =r"C:\Users\Username\Desktop\test\msedgedriver.exe"
options = EdgeOptions()
options.add_argument(r"download.default_directory=C:\Users\username\Desktop\test")
driver = Edge(PATH)
Please can someone advise?
Thank you.
It seems that your code has some issues. You can refer to the steps below to change the download path when automating Edge with Selenium:
Download the correct version of Edge WebDriver from here. Make sure that the Edge WebDriver version is the same as the Edge browser version.
Install the MS Edge Selenium tools using command below:
pip install msedge-selenium-tools selenium==3.141
Run the following sample python code to test:
from msedge.selenium_tools import Edge, EdgeOptions
options = EdgeOptions()
options.use_chromium = True
options.add_experimental_option("prefs", {
"download.default_directory": r"D:\Downloads"
})
driver = Edge(executable_path=r"D:\webdriver\msedgedriver.exe", options=options)
driver.get("https://www.seleniumhq.org/download/");
m = driver.find_element_by_link_text("32 bit Windows IE")
m.click()
Note: Change the paths in the code to your owns.
This is not a complete answer i.e. you will need to experiment a little because there are effects when setting this. Please also note I have had to type this, as I can't copy and paste, therefore a direct copy and paste from this may not work. These are suggestions:
If I change the download folder as below then I find I get an annoying dialog box saying I am signed into Edge and can sync profile settings, with an OK button (and therefore no automatic way to get rid of the dialog). I can find no way of getting rid of this apart from private browsing. So if I want to use SSO, I don't change the download folder to prevent this, if I am manually logging into a webapp I can change the download folder by using private browsing to avoid the dialog. I have searched and searched and cannot find an answer of setting prefs AND NOT having this dialog.
use Selenium 4. msedge-selenium-tools is deprecated and only for selenium 3.x. selenium 4 supports the settings below. In anaconda be careful that by default conda install selenium will give you selenium 3 by default (unlike pip) so check the selenium docs for the conda command to get 4.
imports (there are many more that are useful)
from selenium import webdriver
from selenium.webdriver.edge.options import Options as EdgeOptions
from selenium.webdriver.edge.service import Service as EdgeService
Other useful imports (see docs) ActionChains, expected_conditions,
WebDriverWait, Keys, By, desired_capabilities,staleness_of and I also use
webelement
To set download directory:
edge_options = EdgeOptions()
edge_options.add_experimental_option("prefs", {"download.default_directory":download_path})
You can add other options e.g.:
edge_options.add_experimental_option("prefs, {
"download.default_directory":download_path,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True
})
There are also command line arguments for certain things, but these seem to take a single value. (I also can't yet tell whether you use a single dash, double dash or no dash as different posts give different answers. Example:
options.add_argument("-inprivate") #private browsing with no windows sign in to edge
Then instantiate the driver. Note that many posts use a now deprecated method of running the driver, you should use service and the options variable and then (example):
myService = EdgeService(executable_path = 'path to driver')
myDriver = webdriver.Edge(service=myService, options=edge_options)
element = myDriver.find_element(By.ID, 'foo') #perform selenium functions

How to use existing session with Seleniumbase?

I would like seleniumbase to use my existing session when it launches vs starting a clean one.
How do I start selenium using an existing session?
webdriver:
driver = webdriver.Chrome('/snap/bin/chromium.chromedriver', options=option)
UPDATE:
This is marked as a duplicate which it is not.
Dear #TodorMinakov this code here didn't meet my expectations. I wanted whenever I run the python program to select the unique opened window and use selenium over that window. I will appreciate your help. – Youssof H
Same issue here. The referred URL does not grab an existing session.
You can use default chrome profile.
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=PATH") #)PATH is path to your chrome profile
w = webdriver.Chrome(executable_path="C:\\Users\\chromedriver.exe", chrome_options=options)
To find path to your chrome profile data you need to type chrome://version/ into address bar.
For ex. mine is displayed as C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default,
If path will not work, try to remove \Default\ so we end up with only C:\Users\pc\AppData\Local\Google\Chrome\User Data.
Better to copy-paste default profile outside, because there no way to use same session (cookies) in two browsers in same time.

Selenium: boolean setting in about:config of Firefox webdriver

For a test suite, I'm running a python script controlling a Firefox instance using selenium webdriver. I want to change the setting dom.disable_open_during_load in about:config to true. Although this is the default setting in my default Firefox profile, selenium changes it to false (user-defined) whenever I'm starting a webdriver instance. It seems to use an anonymous, slightly changed profile?! I can then manually change it back, but I was struggling to do it with code: neither using a new profile nor using a pre-set profile configured with Firefox' profile manager solves the problem.
from selenium import webdriver
FFprofile = webdriver.FirefoxProfile()
FFprofile.set_preference('dom.disable_open_during_load', 'true') # I also tried True, 1 - with and without quotes
# FFprofile = webdriver.FirefoxProfile('C:/Users/ExampleUser/AppData/Local/Mozilla/Firefox/Profiles/owieroiuysd.testprofile')
FFdriver = webdriver.Firefox(firefox_profile=FFprofile)
FFdriver.get('http://www.google.com')
I can change various settings this way, but it doesn't work for this one. Where does the changed value false "user-defined" come from? Is it an automatic setting of selenium somewhere? I'm using:
geckodriver 0.16.1
selenium 3.4.2.
Firefox 53.0.3 (64bit)
python 3.4.4
Edit: I just found this question on SO, dealing with the same problem in java.
If this turns out to be impossible, probably there is a nice work-around? Any ideas?
fp = webdriver.FirefoxProfile()
fp.DEFAULT_PREFERENCES['frozen']["dom.disable_open_during_load"] = True
Don't use profile.set_preference('dom.disable_open_during_load', True) as profile.default_preference will be overrided by frozen's.
profile.set_preference('dom.disable_open_during_load', True)
is the correct way to do it, but it won't work for this particular property as it's not allowed to change by user according to the following article. The same thing would work for other parameters.
i.e.
profile.set_preference('browser.download.manager.showWhenStarting', False)
https://www.stigviewer.com/stig/mozilla_firefox/2015-06-30/finding/V-19743
Solution:
create a new profile and directly modify this setting in JS file. and then provide path of this local profile. I have not tested this solution so not sure if it will work or not.
This particular setting seems to be difficult for some reason...
Although I wasn't able to find a solution, I got inspired by this webpage and found a decent work-around using Firefox' developer toolbar:
ActionChains(self.FFdriver).key_down(Keys.SHIFT).send_keys(Keys.F2).key_up(Keys.SHIFT).perform()
time.sleep(0.1) // this seems to be necessary
ActionChains(self.FFdriver).send_keys('pref set dom.disable_open_during_load true').perform()
ActionChains(self.FFdriver).send_keys(Keys.ENTER).perform()
ActionChains(self.FFdriver).key_down(Keys.SHIFT).send_keys(Keys.F2).key_up(Keys.SHIFT).perform()
If anyone should know or find a better way, please comment!

Convert Python code to Robot Framework

I have the python piece of code which needs to be converted to Robot Framework.
Here is the python code..
chromeOptions = webdriver.ChromeOptions()
prefs = {"download.default_directory" : "G:/"}
chromeOptions.add_experimental_option("prefs",prefs)
chromedriver = "C:/Python27/chromedriver.exe"
driver = webdriver.Chrome(executable_path=chromedriver, chrome_options=chromeOptions)
Is it possible to make it work in Robot Framework?
I don't have have much knowledge on Robot Framework.
Using Selenium2Library, a direct translation of that code would look like this:
${chromeOptions}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver
${prefs}= Create Dictionary download.default_directory G:/
Call Method ${chromeOptions} add_experimental_option prefs ${prefs}
${chromedriver}= Set Variable C:/Python27/chromedriver.exe
Create Webdriver Chrome chrome executable_path=${chromedriver} chrome_options=${chromeOptions}
Go To http://someurl/
[Teardown] Close All Browsers
This code depends on two library imports - Selenium2Library and Collections. It worked after adjusting your paths to my system.
Given that you know Python already, I'd direct you to any number of questions asking about how to implement Python in Robot Framework (that coding is mostly on the Python side). Much of the code could likely be simplified by the Open Browser keyword if all you want to do is open a new browser instance to a webpage. To change settings in Chrome, either refer to the questions that explain how to implement your Python code in Robot Framework or use ombre42's suggestions.

Categories

Resources