How to open a Chrome application into Robot using selenium2Library - python

I'm trying to open our windows Chrome exe in Robot using selenium2Library. I've tried creating a webdriver using Create Webdriver with this code but it's not working.
${options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver.chrome.options
${options.add_extension}= Set_Variable path/to/extension
Create WebDriver Chrome chrome_options=${options}
In Python I do this... and it launches my app in selenium.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
def chromedr():
chrome_options = Options()
chrome_options.binary_location = 'C:/Program Files (x86)/InTouch Health/Carestation/Carestation.exe'
driver = webdriver.Chrome('C:/Program Files (x86)/InTouch Health/Carestation/chromedriver.exe', chrome_options=chrome_options)
return driver
How can I do this in Robot/selenium2Library?
I was able to launch the Application using this code:
${options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver Create WebDriver Chrome my_alias chrome_options=${options} executable_path=C:/Program Files (x86)/Myapp.exe

To open chrome with robotframework selenium2library
1) install selenium2Library through pip
2) download and extract Chrome driver exe, add it to environment variables
3) you can invoke chrome like below
*** Settings ***
Library Selenium2Library
*** Variables ***
${SiteUrl} https://stackoverflow.com
${Browser} Chrome
*** Test Cases ***
Test
Open Browser and login
*** Keywords ***
Open Browser and login
open browser ${SiteUrl} ${Browser}

Related

Selenium doesn't load existing profile

I've been trying to update my code, but it seems that I can't make selenium load a profile.
from selenium.webdriver import Firefox
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
profile_path = 'C:/WebDriver/seleniumProfile'
options=Options()
options.set_preference('profile', profile_path)
service = Service('C:/WebDriver/geckodriver.exe')
driver = Firefox(service=service, options=options)
driver.get("https://www.google.com")
The profile has some pre-configured addons and settings.
The code doesn't return an error, but firefox loads a completely clean profile. Tried creating a new virtual environment and tried loading different profiles, but no luck.

Chromedriver for linux32 does not exist - Python Selenium Chromedriver

I'm creating a cross platform python script that executes some commands with selenium.
I have two questions:
How come the following script works on windows but doesn't work on Raspberry pi OS 32bit? The only way this works is to remove the webdriver-manager, but this requires
manual installation of the webdriver.
I'm using a raspberry pi 3
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 webdriver_manager.utils import ChromeType
options = Options()
options.headless = True
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(service=Service(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()), options=options)
driver.get("http://www.google.com")
print(driver.title)
The output is:
pi#raspberrypi:~/Documents/Software $ /bin/python /home/pi/Documents/Software/test.py
====== WebDriver manager ======
Current chromium version is 95.0.4638
Get LATEST chromedriver version for 95.0.4638 chromium
There is no [linux32] chromedriver for browser in cache
Trying to download new driver from https://chromedriver.storage.googleapis.com/95.0.4638.69/chromedriver_linux32.zip
Traceback (most recent call last):
File "/home/pi/Documents/Software/test.py", line 10, in <module>
driver = webdriver.Chrome(service=Service(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()), options=options)
File "/home/pi/.local/lib/python3.9/site-packages/webdriver_manager/chrome.py", line 32, in install
driver_path = self._get_driver_path(self.driver)
File "/home/pi/.local/lib/python3.9/site-packages/webdriver_manager/manager.py", line 30, in _get_driver_path
file = download_file(driver.get_url(), driver.ssl_verify)
File "/home/pi/.local/lib/python3.9/site-packages/webdriver_manager/utils.py", line 98, in download_file
validate_response(response)
File "/home/pi/.local/lib/python3.9/site-packages/webdriver_manager/utils.py", line 80, in validate_response
raise ValueError("There is no such driver by url {}".format(resp.url))
ValueError: There is no such driver by url https://chromedriver.storage.googleapis.com/95.0.4638.69/chromedriver_linux32.zip
How can I create a python script that uses selenium webdriver in headless mode and works on every platform? I mean, if I use chromewebdriver in the script, the user who will use the script must have chrome installed, as well as if a firefox the user must have firefox installed. Is there any webdriver that works without external script installations?
Thanks
EDIT:
The problem is not with the webdriver manager but the fact that chromedrivers for chromium do not exist for linux32. In fact at the address: "https://chromedriver.storage.googleapis.com/95.0.4638.69/chromedriver_linux32.zip" there is no chromedriver, but replacing linux32 with linux64 a package is downloaded but not compatible with linux32.
The thing I don't understand is if the chromedrivers for linux32 don't exist then why installing them with: "sudo apt-get install chromium-chromedriver" and then removing the webdriver-manager calls from the code, does the python script work? Then there are chromedrivers for linux32, only they are not present in the main chromedriver site.
I am using a raspberry pi 3 with chromium 95.0.4638.69.
As there is no linux32 chromedriver developped by the official team (e.g. https://chromedriver.storage.googleapis.com/index.html?path=99.0.4844.17/), you can't use webdriver_manager because it's looking at their repositories.
Yet a compatible chromedriver version is maintained by the Raspian team, so in order to make it works you need to :
Install chromedriver for raspberry. e.g. for Linux:
sudo apt-get install chromium-chromedriver
When you instantiate your driver, you need to tell him where to find this chromedriver. Plus, you should also state that you use Chromium instead of Chrome (if so):
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
options = Options()
# browser is Chromium instead of Chrome
options.BinaryLocation = "/usr/bin/chromium-browser"
# we use custom chromedriver for raspberry
driver_path = "/usr/bin/chromedriver"
driver = webdriver.Chrome(options=options, service=Service(driver_path))
Then you're good to go:
driver.get("https://stackoverflow.com/")
If you need a code that works for every platform, the best option I found so far is to distinguished raspberry pi from the other system:
import platform
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
options = Options()
# a few usefull options
options.add_argument("--disable-infobars")
options.add_argument("start-maximized")
options.add_argument("--disable-extensions")
options.add_argument("--headless") # if you want it headless
if platform.system() == "Linux" and platform.machine() == "armv7l":
# if raspi
options.BinaryLocation = ("/usr/bin/chromium-browser")
service = Service("/usr/bin/chromedriver")
else: # if not raspi and considering you're using Chrome
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(
service=service,
options=options
)
Considering your question about not having to install Chrome manually to make Selenium works, you could probably install Chrome through a script to be sure that the user has it.
It will also work for a Docker use, e.g. for a DockerFile with a Linux amd64 environment:
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy instal

Python executable_path keyword no longer recognized when trying to launch chrome driver [duplicate]

I am using sublime to code python scripts. The following code is for selenium in python to install the driver automatically by using the webdriver_manager package
# pip install webdriver-manager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
#s=Service(path)
#driver=webdriver.Chrome(service=s)
driver.get('https://www.google.com')
driver.find_element(By.NAME, 'q').send_keys('Yasser Khalil')
The code works fine but I got a warning like that
Demo.py:7: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome(ChromeDriverManager().install())
How to fix such a bug?
This error message...
DeprecationWarning: executable_path has been deprecated, please pass in a Service object
...implies that the key executable_path will be deprecated in the upcoming releases.
This change is inline with the Selenium 4.0 Beta 1 changelog which mentions:
Deprecate all but Options and Service arguments in driver instantiation. (#9125,#9128)
Solution
With selenium4 as the key executable_path is deprecated you have to use an instance of the Service() class along with ChromeDriverManager().install() command as discussed below.
Pre-requisites
Ensure that:
Selenium is upgraded to v4.0.0
pip3 install -U selenium
Webdriver Manager for Python is installed
pip3 install webdriver-manager
You can find a detailed discussion on installing Webdriver Manager for Python in ModuleNotFoundError: No module named 'webdriver_manager' error even after installing webdrivermanager
Selenium v4 compatible Code Block
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.google.com")
Console Output:
[WDM] - ====== WebDriver manager ======
[WDM] - Current google-chrome version is 96.0.4664
[WDM] - Get LATEST driver version for 96.0.4664
[WDM] - Driver [C:\Users\Admin\.wdm\drivers\chromedriver\win32\96.0.4664.45\chromedriver.exe] found in cache
You can find a detailed discussion on installing Webdriver Manager for Python in Selenium ChromeDriver issue using Webdriver Manager for Python
Incase you want to pass the Options() object you can use:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
options = Options()
options.add_argument("start-maximized")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get("https://www.google.com")
TL; DR
You can find the relevant Bug Report/Pull Request in:
Bug Report: deprecate all but Options and Service arguments in driver instantiation
Pull Request: deprecate all but Options and Service arguments in driver instantiation
This works for me
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
ser = Service(r"C:\chromedriver.exe")
op = webdriver.ChromeOptions()
s = webdriver.Chrome(service=ser, options=op)
Extending on the accepted answer, the Service class allows to explicitly specify a ChromeDriver executable in the same way as previously using the executable_path parameter. In this way existing code is easily migrated (clearly you need to replace C:\chromedriver.exe above by your path).
I could figure it out
# pip install webdriver-manager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
s=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)
driver.maximize_window()
driver.get('https://www.google.com')
driver.find_element(By.NAME, 'q').send_keys('Yasser Khalil')
I found this deprecation issue is appearing on Selenium, Pip and Python updates. so simply just change :
before:
from selenium import webdriver
chrome_driver_path = 'C:/Users/Morteza/Documents/Dev/chromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_driver_path)
url = "https://www.google.com"
driver.get(url)
after:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
s=Service('C:/Users/Morteza/Documents/Dev/chromedriver.exe')
browser = webdriver.Chrome(service=s)
url='https://www.google.com'
browser.get(url)
All the above answers refer to Chrome, adding the one for Firefox
Install:
pip install webdriver-manager
Code:
from selenium.webdriver.firefox.service import Service
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Firefox(service=Service(executable_path=GeckoDriverManager().install()))
Reference: https://github.com/SergeyPirogov/webdriver_manager/issues/262#issuecomment-955197860
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service_obj = Service("WebDrivers_path\chromedriver.exe")
driver = webdriver.Chrome(service=service_obj)
driver.get("https://www.google.com")
Simplest option with Chrome auto-installer:
from selenium import webdriver
import chromedriver_autoinstaller
from selenium.webdriver.chrome.service import Service
chromedriver_autoinstaller.install()
driver = webdriver.Chrome(service=Service())
Have a look at the new definition in the Service object here.
My solution
from selenium.webdriver.chrome.service import Service
chrome_executable = Service(executable_path='chromedriver.exe', log_path='NUL')
driver = webdriver.Chrome(service=chrome_executable)
if you are using any IDE like PyCharm install webdriver-manager package of that IDE as how do install for selenium package
You can create an instance of ChromeOptions, which has convenient methods for setting ChromeDriver-specific capabilities. You can then pass the ChromeOptions object into the ChromeDriver constructor:
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
ChromeDriver driver = new ChromeDriver(options);
Since Selenium version 3.6.0, the ChromeOptions class in Java also implements the Capabilities interface, allowing you to specify other WebDriver capabilities not specific to ChromeDriver.
ChromeOptions options = new ChromeOptions();
// Add the WebDriver proxy capability.
Proxy proxy = new Proxy();
proxy.setHttpProxy("myhttpproxy:3337");
options.setCapability("proxy", proxy);
// Add a ChromeDriver-specific capability.
options.addExtensions(new File("/path/to/extension.crx"));
ChromeDriver driver = new ChromeDriver(options);

How to auto download .exe files to project directory based on browser version using Webdriver Manager in Python Robotframework

I tried below code but its downloaded and save it the path we configured in pip list [2nd line of code]. Instead of auto download in my local machine, I want to download directly into project directory because of few restriction issues in my organisation. Can anyone provide suggestions on this?
pip install webdrivermanager
webdrivermanager firefox chrome --linkpath /usr/local/bin
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
def get_chromedriver_path():
driver_path = ChromeDriverManager().install()
print(driver_path)
return driver_path
Library chromedriversync.py
${chromedriver_path}= chromedriversync.Get Chromedriver Path
Create Webdriver chrome executable_path=${chromedriver_path}
Go to www.google.com
Finally I got the solution. Below query helped me to auto download .exe files of the respective browser version to Project Current directory.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from lib2to3.tests.support import driver
import os
import os.path
from os.path import curdir
def get_chromedriver_path():
ReturnPath = os.getcwd()
driver_path = ChromeDriverManager(path=ReturnPath).install()
print(driver_path)
return driver_path
Library browser
${driver}= browser.Get Chromedriver Path
log ${driver}
Create Webdriver Chrome executable_path=${driver} chrome_options=${options}

python using selenium, error: chrome unexpectedly exited. Status code was: 0

This is not a repost of
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed with ChromeDriver and Selenium in Python
I am using Linux and creating a new profile is not an option. I want to load an existing profile (not create a new one) just like selenium gui can.
I am able to get chromium to function, but not google chrome. Chrome will open but will kick back an
selenium.common.exceptions.WebDriverException: Message: Service /opt/google/chrome/chrome unexpectedly exited. Status code was: 0
error.
I am trying to start google chrome with user directory access, so I can capture existing sessions.
code that fails:
option.add_argument("user-data-dir=/home/user/.config/google-chrome/Default/") #)PATH is path to your chrome profile
driver = webdriver.Chrome('/opt/google/chrome/chrome', options=option)
code that works but launches chromium and not google-chrome:
option.add_argument("user-data-dir=/home/user/snap/chromium/common/.cache/chromium/Default/") #)PATH is path to your>
driver = webdriver.Chrome('/snap/bin/chromium.chromedriver', options=option)
I'm sure I am using the correct executable
I'm pretty sure I have the correct chromedriver driver install
root#Inspiron-laptop:/home/user# pip3 install chromedriver-autoinstaller
Requirement already satisfied: chromedriver-autoinstaller in /usr/local/lib/python3.8/dist-packages (0.2.2)
Just using it incorrectly.
How do I launch google-chrome from within selenium while accessing cache directories?
I am on Ubuntu 20.04
UPDATE:
Full script:
#!/usr/bin/python3
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 import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from seleniumbase import BaseCase
from selenium.webdriver.chrome.options import Options
import time
import random
minptime = 25
maxptime = 120
class MyweClass(BaseCase):
def method_a():
option = webdriver.ChromeOptions()
option.add_argument('--disable-notifications')
option.add_argument("--mute-audio")
option.add_argument("user-data-dir=/home/user/.config/google-chrome/Default/") #)PATH is path to your chrome profile
driver = webdriver.Chrome('/opt/google/chrome/chrome', options=option)
driver.get("https://world.com/myworld")
print(f'driver.command_executor._url: {driver.command_executor._url}')
print(f'driver.session_id: {driver.session_id}')
time.sleep(18)
return driver
driver = MyweClass.method_a()
UPDATE II:
Same error using
option.add_argument("user-data-dir=~/.config/google-chrome/Default/")
and
driver = webdriver.Chrome('/opt/google/chrome/google-chrome', options=option)
and
chmod -R 777 /home/user/.config
To ensure user was hitting cache directory as user.
Google chrome info:
Thumb rule
A common cause for Chrome to crash during startup is running Chrome as root user (administrator) on Linux. While it is possible to work around this issue by passing --no-sandbox flag when creating your WebDriver session, such a configuration is unsupported and highly discouraged. You need to configure your environment to run Chrome as a regular user instead.
This error message...
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
(Driver info: chromedriver=2.26.436382 (70eb799287ce4c2208441fc057053a5b07ceabac),platform=Linux 4.15.0-109-generic x86_64)
...implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context i.e. Chrome Browser session.
As per your code trials seems you are trying to access a Chrome Profile so you can use the following solution:
Code Block:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("user-data-dir=C:\\path\\to\\your\\profile\\Google\\Chrome\\User Data\\Profile 2")
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.co.in")
References
You can find a couple of detailed discussions in:
How to open a Chrome Profile through Python
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed with ChromeDriver and Selenium in Python
How to use Chrome Profile in Selenium Webdriver Python 3
Selenium: Point towards default Chrome session

Categories