Is it possible to port this python code to work in Robot Framework?
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
chrome_options = Options()
chrome_options.binary_location = 'C:/Program Files (x86)/MyApp.exe'
driver = webdriver.Chrome('C:/Program Files (x86)/chromedriver.exe', chrome_options=chrome_options)
I'm trying to create a chrome webdriver in robot that sends my selenium calls to my chromium application. Is it possible?
I create a python Library see code below but it just launches my app and closes it. I want to be able to make selenium/robot calls to it.
Code for myLibrary.py
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from robot.libraries.BuiltIn import BuiltIn
def pas_webdriver_instance():
se2lib = BuiltIn().get_library_instance('ExtendedSelenium2Library')
chrome_options = Options()
chrome_options.binary_location = 'C:/Program Files (x86)/myapp.exe'
driver = webdriver.Chrome('C:/Program Files (x86)/chromedriver.exe', chrome_options=chrome_options)
You could simply expand Selenium2Library and use your Python code directly. To expand Selenium2Library you can either make a new class that inherits the original library, or get the runnung library's instance on runtime.
Or, if you absolutely want to port this into Robot Framework, you can try out this keyword:
Open Chrome
[Arguments] ${url} ${binary_path}
${chrome_options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver
Call Method ${chrome_options} binary_location ${binary_path}
Create Webdriver Chrome chrome_options=${chrome_options}
Go To ${url}
Edit: To answer your question in comments,
That depends on the version of Selenium2Library you're using since the library got reworked heavily in it's last version, but if you're using versions prior to it's version 3, you can see an example of expansion that gets an instance here (see the property _s2l), and an example of class inheritance here (see the declaration of the class).
I haven't looked into expanding the last version of SeleniumLibrary yet so I couldn't tell you for this particular case. Retrieving the library's instance on runtime should still work fine though.
Related
I would like to use an existing installation of chrome (or firefox or brave browser) with selenium. Like that I could set prespecified settings / extensions (e.g. start nord-vpn when opening a new instance) that are active when the browser is opened with selenium.
I know there is selenium.webdriver.service with the "executeable-path" option, but it doesn't seem to work when you specify a specific chrome.exe, the usage seems to be for the chrome-driver only and then it still opens a "fresh" installation of chrome.
Starting selenium with extension-file I think is also not an option to use with the nord-vpn extension, as I have two-factor authentication active and login every single time would take too much time and effort, if possible at all.
Firefox profile
To use the existing installation of firefox you have to pass the profile path through set_preference() method using an instance of Option from selenium.webdriver.common.options as follows:
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 = r'C:\Users\Admin\AppData\Roaming\Mozilla\Firefox\Profiles\s8543x41.default-release'
options=Options()
options.set_preference('profile', profile_path)
service = Service('C:\\BrowserDrivers\\geckodriver.exe')
driver = Firefox(service=service, options=options)
driver.get("https://www.google.com")
You can find a relevant detailed discussion in Error update preferences in Firefox profile: 'Options' object has no attribute 'update_preferences'
Chrome profile
Where as to use an existing installation of google-chrome you have to pass the user profile path through add_argument() using the user-data-dir key through an instance of Option from selenium.webdriver.common.options as follows:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
options = Options()
options.add_argument("user-data-dir=C:\\Users\\username\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get("https://www.google.com/")
You can find a relevant detailed discussion in How to open a Chrome Profile through Python
I have been trying to work on my first web-scraping project for which I am using Selenium. However, I seem to be running into some issues with importing the ChromeDriver. I am using Selenium 3.0.0 and am working on Chrome.
webdriver_service = Service(ChromeDriverManager().install())
chrome_options = Options()
chrome_options.add_argument("--headless") # Ensure GUI is off
chrome_options.add_argument("--no-sandbox")
# Silent download of drivers
logging.getLogger('WDM').setLevel(logging.NOTSET)
os.environ['WDM_LOG'] = 'False'
driver = webdriver.Chrome(executable_path='/Users/MyUsername/Downloads/chromedriver.exe')
I keep getting the following message: 'chromedriver.exe' executable needs to be in PATH.
Let me know if there's some issue with the file path I am using as I think that is where the issue is coming from.
As answer tells, you should specify the chromedriver-path as following:
driver = webdriver.Chrome('/Users/MyUsername/Downloads/chromedriver.exe')
The executable_path= argument stands for chrome.exe, meaning google-chrome browser.
This should be done through the OS when possible and not through code as it needs to be done for every project this way but it is possible to set environment variables through code
import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
# Setting said environment variable
os.environ["CHROME_DRIVER_PATH"] = "Z:\\port\\driver\\chromedriver"
# Reading variable we created
DRIVER_PATH = os.environ.get('CHROME_DRIVER_PATH')
s=Service(DRIVER_PATH)
Got my answer from here
I'm going to be honest and say the path looks good to me. I even tried to run it in Visual Studio and it worked perfectly, so I have no idea why it isn't working for you. That being said I do have a way you can fix it. You can just not use executable_path. You already imported webdriver whose sole purpose is to handle the webdriver for selenium for you.
See here.
You don't even have to change much.
#pip install webdriver-manager, pip install selenium==3.0.0
import os
import logging
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
chrome_options = Options()
chrome_options.add_argument("--headless") # Ensure GUI is off
chrome_options.add_argument("--no-sandbox")
# Silent download of drivers
logging.getLogger('WDM').setLevel(logging.NOTSET)
os.environ['WDM_LOG'] = 'False'
driver = webdriver.Chrome(ChromeDriverManager().install())
#driver = webdriver.Chrome(executable_path='/Users/MyUsername/Downloads/chromedriver.exe')
driver.get('https://www.google.com/')
Hope this helps.
You have to take care of a couple of things:
As you are using Selenium 3.0.0 you don't have to create any Service() object. So you can remove the line:
webdriver_service = Service(ChromeDriverManager().install())
Incase you don't intend to use ChromeDriverManager() you can remove the following lines:
# Silent download of drivers
logging.getLogger('WDM').setLevel(logging.NOTSET)
os.environ['WDM_LOG'] = 'False'
Unless you are executing your test in headless mode you won't require the argument --headless. So you can remove the line:
chrome_options.add_argument("--headless") # Ensure GUI is off
Unless you are executing your test as a root/administrator you won't nee the argument --no-sandbox. So you can remove the line:
chrome_options.add_argument("--no-sandbox")
Now you can download the matching ChromeDriver version from ChromeDriver page, unzip/untar it and use the following lines of code:
from selenium import webdriver
driver = webdriver.Chrome(executable_path='/path/to/chromedriver') # for windows OS use chromedriver.exe
driver.get('https://www.google.com/')
I'm relatively new to Winium (1.6.0.0) , Selenium (4.1.3) and Python (3.10.2).
I want to automate a simple app like calculator
I implemented some suggested code but it's for an older version of Selenium
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
import os
os.startfile(r'c:\\test\\winium.desktop.driver.exe')
driver = webdriver.Remote(command_executor='http://localhost:9999', desired_capabilities={'app': "C:\\Windows\\System32\\calc.exe","args": '-port 345'})
I get the following error from Pycharm:
DeprecationWarning: desired_capabilities has been deprecated, please pass in an Options object with options kwarg*
driver = webdriver.Remote(command_executor='http://localhost:9999', desired_capabilities={'app': "C:\\Windows\\System32\\calc.exe","args": '-port 345'})
How do I update the code to use an Options object instead?
thanks
I tried adjusting the options, but I am uncertain to what options I should be using to test a windows application
I'm looking at this code:
#! python3
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
opts = Options()
opts.set_headless()
assert opts.headless # Operating in headless mode
browser = Firefox(options=opts)
browser.get('https://duckduckgo.com')
source: https://realpython.com/modern-web-automation-with-python-and-selenium/
the idea is to call a headless browser but I don't understand the logic behind this code. What is 'options', and what is 'Options'? What do they exactly do? what options=opts stands for?
Now trying to run this code and the webpage duckduckgo won't open. Any idea why?
Options is a class in the selenium firefox webdriver package.
opts is an instance of the Options class instantiated for the program.
When the code says:
opts = Options()
Python creates an instance of the class, and uses the the variable opts as the access point.
When the code says:
opts.set_headless()
Python is updating the instance of Options, to store the information “the user of this wants to start a headless instance of the browser”
When the code says:
browser = Firefox(options=opts)
Python is creating an instance of the Firefox class, and sending it the opts variable to configure the new instance. In this case, the only option that has been modified from the defaults is the headless flag.
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import time
#--| Setup
options = Options()
options.add_argument("--headless")
caps = webdriver.DesiredCapabilities().FIREFOX
caps["marionette"] = True
browser = webdriver.Firefox(firefox_options=options, capabilities=caps, executable_path=r"geckodriver.exe")
#--| Parse
browser.get('https://duckduckgo.com')
logo = browser.find_elements_by_css_selector('#logo_homepage_link')
print(logo[0].text)
this code works ( gives output About DuckDuckGo ). I was told that opts.set_headless() is deprecated, maybe that's why it didn't give me any result.
Is it possible to connect selenium to the browser I use normally instead a driver? For normal browsing I am using chrome with several plugins - add block plus, flashblock and several more. I want to try to load a site using this specific configuration. How can I do that?
p.s - I dont want to connect only to an open browser like in this question :
How to connect to an already open browser?
I dont care if I spawn the process using a driver. I just want the full browser configuration - cookies,plugins,fonts etc.
Thanks
First, you need to download the ChromeDriver, then either put the path to the executeable to the PATH environment variable, or pass the path in the executable_path argument:
from selenium import webdriver
driver = webdriver.Chrome(executable_path='/path/to/executeable/chrome/driver')
In order to load extensions, you would need to set ChromeOptions:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_extension('Adblock-Plus_v1.4.1.crx')
driver = webdriver.Chrome(chrome_options=options)
You can also save the chrome user profile you have and load it to the ChromeDriver:
options = webdriver.ChromeOptions()
options.add_argument('--user-data-dir=/path/to/my/profile')
driver = webdriver.Chrome(chrome_options=options)
See also:
Running Selenium WebDriver using Python with extensions (.crx files)
ChromeDriver capabilities/options