Roboframework - SeleniumLibrary - set specific user profile path for Chrome - selenium

my goal is to create a Roboframework script using SeleniumLibrary (or Selenium2Library), selecting a specific Profile Path for Chrome (I need to reuse profile).
This is the code I'm using:
*** Settings ***
Library Selenium2Library
Library OperatingSystem
*** Test Cases ***
Test Profile
${options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver
${options.add_argument}= Set Variable --user-data-dir=/tmp/.org.chromium.Chromium.Test
Create WebDriver Chrome chrome_options=${options}
Go To chrome://version
Wait Until Page Contains JavaScript
Capture Page Screenshot
If I run the script I don't receive any error, but unfortunately the chrome path is not effected by the desired setting (all the time is creating root+random path):
In the past I've successfully achieve this using Python code and Selenium Grid:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-notifications')
chrome_options.add_argument('--user-data-dir=/tmp/.com.google.Chrome.Test')
browser = webdriver.Remote(command_executor='http://192.168.99.100:4444/wd/hub',desired_capabilities = chrome_options.to_capabilities())
Any help/comment is appreciated.
Many Thanks

Try something like below
*** Settings ***
Library Selenium2Library
Library OperatingSystem
*** Test Cases ***
Test Profile
${options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver
Call Method ${options} add_argument --user-data-dir=/tmp/.org.chromium.Chromium.Test
Create WebDriver Chrome chrome_options=${options}
Go To chrome://version
Wait Until Page Contains JavaScript
Capture Page Screenshot
If that doesn't work try without the --, i.e.
Try something like below
*** Settings ***
Library Selenium2Library
Library OperatingSystem
*** Test Cases ***
Test Profile
${options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver
Call Method ${options} add_argument user-data-dir=/tmp/.org.chromium.Chromium.Test
Create WebDriver Chrome chrome_options=${options}
Go To chrome://version
Wait Until Page Contains JavaScript
Capture Page Screenshot

I made it work with this syntax:
${chrome_options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys
Call Method ${chrome options} add_argument user-data-dir\=/tmp/.org.chromium.Chromium.Test
${dc} Evaluate sys.modules['selenium.webdriver'].DesiredCapabilities.CHROME sys, selenium.webdriver
${Options}= Call Method ${ChromeOptions} to_capabilities
Create WebDriver Chrome chrome_options=${chrome_options} desired_capabilities=${dc}
Go To chrome://version
Wait Until Page Contains JavaScript
Capture Page Screenshot

Related

TimeoutException: Message: Timed out waiting for page to load

I want to Open browser in Edge with IE mode.
My environment: IE7, windows 11, Python 3.10.4, Edge version 108.0.1462.46
And I follow the required configuration from below:
https://www.selenium.dev/documentation/ie_driver_server/
I made the same value for Enhanced Protected Mode by setting the REG_DWORD 2500 value to 0 in Zones 0,1,2,3,4:
Registry Editer path: Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones
I add the IEDriverServer to my PATH
Is there any wrong steps about my configuration?
Below is my code:
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${IEDriver} D:\\IEDriver\\64bits\\IEDriverServer.exe
*** Test Cases ***
Example Test
Open Browser https://www.google.com.tw/ ie executable_path=${IEDriver} options=ignore_zoom_level=True; attach_to_edge_chrome=True; edge_executable_path="C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"
Maximize Browser Window
After I executing my code, I can open google in Edge with IE mode, but after that web page stuck and always get thie error message:
TimeoutException: Message: Timed out waiting for page to load.
And I found something interesting: I use python and selenium and below is my code:
ieOptions = webdriver.IeOptions()
ieOptions.add_additional_option("ie.edgechromium", True)
ieOptions.add_additional_option("ie.edgepath",'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe')
driver = webdriver.Ie(options=ieOptions)
driver.maximize_window()
driver.get('https://google.com')
driver.quit()
If I remove this line "driver.get('https://google.com')" and my code runs perfectly.
But If I add it back, the page will go to goole and stuck there (It means that this code will not do driver.quit()
This option may work for you in Windows 11 IE mode automation. Check it out:
ignore_protected_mode_settings = True
UPDATE
Or you can simply try setting the page load timeout according to your requirement:
*** Variables ***
${orig timeout} 15 seconds
*** Test Cases ***
Example Test
Set Selenium Timeout ${orig timeout}
If the element you'd like to interact with has been fully loaded, you can skip the page loading and continue. A native Python example:
driver.set_page_load_timeout(10)
try:
driver.get('https://google.com')
except TimeoutException:
print
'!!!!!!time out after 10 seconds!!!!!!'
driver.execute_script("window.stop()")
In Robot Framework:
*** Test Cases ***
Example Test
Set Selenium Timeout ${orig timeout}
TRY
Open Browser https://www.google.com ie options=ignore_zoom_level=True; attach_to_edge_chrome=True; edge_executable_path="C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"
EXCEPT '!!!!!!time out after 10 seconds!!!!!!'
Execute Javascript window.stop()
END
Maximize Browser Window

How to enable adblock with seleniumlibrary in robotframework

I would like to enable uBlock Origin plugin as ads on speedtest.net during robot framework test execution as ads completly jeopardize test result.
So far I have created a Python script to create a profile with extension
from selenium import webdriver
class WebDriverProfile:
def create_profile_with_adblock(self, path):
fp =webdriver.FirefoxProfile()
fp.add_extension(extension='d:/pathtoextension/ublock_origin-1.24.0-an+fx.xpi')
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir",path)
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", 'application/csv')
fp.update_preferences()
return fp.path
Also the following robot script to call the function:
*** settings ***
Library Selenium2Library
library Process
library Dialogs
Library WebDriverProfile.py
*** Variables ***
${URL} https://speedtest.net
*** Test Cases ***
Test with speedtest.net
${FF_PROFILE}= Create Profile With Adblock ${TEMPDIR}\\testdirff
Open Browser ${URL} browser=ff ff_profile_dir=${FF_PROFILE}
Execute Manual Step Is adblocker enabled?
The browser will open, but adblocker won't be there.
Anyone could help on this please?
Create webdriver instead of firefox profile and install add-on.
Python extension:
from selenium import webdriver
class WebDriverProfile:
def create_web_driver_with_addons(self):
extension_location='d:\\path_to_adblock\\ublock_origin-1.24.0-an+fx.xpi'
browser = webdriver.Firefox()
browser.install_addon(extension_location, temporary=True)
browser.get('http://speedtest.net')
Robot script:
*** settings ***
Library Selenium2Library
library Process
library Dialogs
Library WebDriverProfile.py
*** Variables ***
${URL} https://speedtest.net
*** Test Cases ***
Test with speedtest.net
create web driver with addons
Execute Manual Step Is adblocker enabled?
You need to create a new browser profile with adblock manually installed firest.
Now, Call the above respective profile the by anyone of the below commands.
Open Browser http://example.com Firefox ff_profile_dir=/path/to/profile # Using profile from disk
Open Browser http://example.com Firefox ff_profile_dir=${FirefoxProfile_instance} # Using instance of FirefoxProfile
Open Browser http://example.com Firefox ff_profile_dir=set_preference("key", "value");set_preference("other", "setting") # Defining profile using FirefoxProfile mehtods~~~

selenium.common.exceptions.SessionNotCreatedException: Message: session not created: No matching capabilities error with ChromeDriver Chrome Selenium

First, machine and package specs:
I am running:
ChromeDriver version 75.0.3770.140
Selenium: version '3.141.0'
WSL (linux subsystem) of windows 10
I am trying to run a chromebrowser through selenium. I found: these commands, to use selenium through google chrome.
I have a test directory, with only the chromedriver binary file, and the script, in it. The location of the directory is: /home/kela/test_dir/
I ran the code:
import selenium
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
options = Options()
options.binary_location='/home/kela/test_dir/chromedriver'
driver = webdriver.Chrome(chrome_options = options,executable_path='/home/kela/test_dir/chromedriver')
The output from this code is:
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: No matching capabilities found
Can anyone explain why I need capabilities when the same script works for others without capabilities? I did try adding:
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
but I got the same error. So I'm not sure what capabilities I need to add (considering it works for others without it?)
Edit 1: Addressing DebanjanB's comments below:
Chromedriver is in the expected location. I am using windows 10. From here, the expected location is C:\Program Files (x86)\Google\Chrome\Application\chrome.exe; and this is where it is on my machine (I copied and pasted this location from the chrome Properties table).
ChromeDriver is having executable permission for non-root users.
I definitely have Google Chrome v75.0 installed (I can see that the Product version 75.0.3770.100)
I am running the script as a non-root user, as my bash command line ends with a $ and not # (i.e kela:~/test_dir$ and not kela:~/test_dir#)
Edit 2: Based on DebanjanB's answer below, I am very close to having it working, but just not quite.
The code:
import selenium
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.options import Options
options = Options()
options.binary_location='/c/Program Files (x86)/Google/Chrome/Application/chrome.exe'
driver = webdriver.Chrome(options=options)
driver.get('http://google.com/')
Produces a dialog box that reads:
Google Chrome cannot read and write to it's data directory: /tmp/.com/google.Chrom.gyw63s
So then I double checked my Chrome permissions and I should be able to write to Chrome:
Also, I can see that /tmp/ has a bunch of .com dirs in it:
.com.google.Chrome.4jnWme/ .com.google.Chrome.FdNyKP/ .com.google.Chrome.VAcWMQ/ .com.google.Chrome.ZbkRx0/ .com.google.Chrome.iRrceF/
.com.google.Chrome.A2QHHB/ .com.google.Chrome.G7Y51c/ .com.google.Chrome.WD8BtK/ .com.google.Chrome.cItmhA/ .com.google.Chrome.pm28hN/
However, since that seemed to be more of a warning than an error, I clicked 'ok' to close the dialog box, and a new tab does open in the browser; but the URL is just 'data:,'. The same thing happens if I remove the line 'driver.get('http://google.com')' from the script, so I know the warning/issue is with the line:
driver = webdriver.Chrome(chrome_options = options,executable_path='/home/kela/test_dir/chromedriver')
For example, from here, I tried adding:
options.add_argument('--profile-directory=Default')
But the same warning pops up.
Edit 3:
As edit 3 was starting to veer into a different question than specifically being addressed here, I started a new question here.
This error message...
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: No matching capabilities found
...implies that the ChromeDriver was unable to initiate/spawn a new WebBrowser i.e. Chrome Browser session.
binary_location
binary_location set/get(s) the location of the Chrome (executable) binary and is defined as:
def binary_location(self, value):
"""
Allows you to set where the chromium binary lives
:Args:
- value: path to the Chromium binary
"""
self._binary_location = value
So as per your code trials, options.binary_location='/home/kela/test_dir/chromedriver' is incorrect.
Solution
If Chrome is installed at the default location, you can safely remove this property. Incase Chrome is installed at a customized location you need to use the options.binary_location property to point to the Chrome installation.
You can find a detailed discussion in Selenium: WebDriverException:Chrome failed to start: crashed as google-chrome is no longer running so ChromeDriver is assuming that Chrome has crashed
Effectively, you code block will be:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.binary_location=r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
driver = webdriver.Chrome(options=options, executable_path='/home/kela/test_dir/chromedriver.exe')
driver.get('http://google.com/')
Additionally, ensure the following:
ChromeDriver is having executable permission for non-root users.
As you are using ChromeDriver v75.0 ensure that you have the recommended version of the Google Chrome v75.0 as:
---------ChromeDriver 75.0.3770.8 (2019-04-29)---------
Supports Chrome version 75
Execute the Selenium Test as non-root user.

ModHeader Chrome Extenstion with Robot Framework

I am learning to use Robot Framework together with Selenium. I use the Chrome Extension "ModHeader" and I have managed to export it to a crx "zip" file, and then import it in my robot test case:
${EXTENSTION PATH} ./modheader_extension.crx
${chrome options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver
Call Method ${chrome options} add_extension ${EXTENSTION PATH}
Create Webdriver Chrome chrome_options=${chrome options}
Goto ${BASE URL}
The problem arises that the extension is loaded but without any settings, ie, no headers configured.
I need one header, with a key and value set, could someone please help on how this should be done? I have tried to look at documentation and other stack overflow questions. I am new to both Robot framework and python.
Best Regards
Alexander
You can use the extensions built in https://github.com/bewisse/modheader_selenium, and then navigate to https://bewisse.com/add?Test=1 to add the header Test: 1.

Selenium: How to make geckodriver headless

I have completed code for geckodriver and was wondering how to make geckodriver headless now. I saw a post previously with the following text:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.add_argument("--headless")
driver = webdriver.Firefox(firefox_options=options,
executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")
print("Firefox Headless Browser Invoked")
driver.get('http://google.com/')
driver.quit()
I don't understand where the download for options came from under webdriver. When I downloaded geckodriver, all that came with it was the executable file. Any help is greatly appreciated!!
Works for me. Steps I used: (1) Open a command prompt and navigate to the folder containing geckodriver.exe. (2) Start geckodriver.exe without any options from a command prompt. (3) Open another command prompt and type python and press the Return key. (4) Copy/paste the following code into your your python session.
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver import Firefox
options = Options()
options.add_argument("--headless")
# Don't put the path to geckodriver in the following. But the firefox executable
# must be in the path. If not, include the path to firefox, not geckodriver below.
driver = Firefox(firefox_options=options)
print("Firefox Headless Browser Invoked")
driver.get('http://google.com/')
# Print the first 300 characters on the page.
print(driver.page_source[:300])
driver.quit()