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.
Related
So I currently have code that works using chrome and webdrivermanager. However I was wondering if it is possible to convert the browser that Selenium uses from Chrome to Edge?
Is it as simple as a single line of code (redefining "driver" variable)? Or do I have to rewrite things completely?
The language is Python.
You must install Microsoft Edge WebDriver (https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/).
After that, you can use it like this :
driver = webdriver.Edge(r"path/msedgedriver.exe")
I'm new in IT and automation QA, I have a trouble in using Selenium library and SeleniumLibrary in RobotFramework. As I know, the SeleniumLibrary will support using method as Keyword in Robot Framework (RF).
Suppose in *.py file I can use Selenium to call open browser:
driver = webdriver.Chrome() driver.get("https://google.com") ...
and in *.robot I open a browser with SeleniumLibrary like:
open browser https://google.com
While researching for many solutions, I found many tutorials in Selenium and it was easy to understand and apply, but in my project, they's been extending the SeleniumLibrary by:
driver = SeleniumLibrary
driver.open_browser("https://google.com")
= driver.get("https://google.com") in *.py (from selenium import webdriver)
I feel hard to read the source code of SeleniumLibrary e.g If I need to use
find_element(By.ID/CSS/etc.., "abc") in *.py and in SeleniumLibrary I only searched that
def find_element(
self,
locator: str,
tag: Optional[str] = None,
required: bool = True,
parent: WebElement = None,
)
I dont how to write code like before with selenium.webdriver..
Does anyone help me to clear them? How can I use driver.find_element (of SeleniumLibrary) in *.py file that equals driver.find_element_by_CSS_selector (of Selenium.webdriver)
There're various strategies how to locate elements on a page, read about them in the documentation here.
The point is that you want to do something with the element you locate, in Python, you'd typically do it in two steps, first find the element and then click on it (for example). In Robot Framework, you do it in one step like so:
Click Element id:foo
That's all. Two steps are taken care of here:
finding the element in the DOM
clicking on the element
Robot Framework is a bit more high-level than Python, so many tasks in it will require fewer lines of code. I think this is what you're asking about and are not really used to yet. I recommend using RF on a small project to learn it. You always have documentation ready on the Internet.
from selenium import webdriver
browser = webdriver.Chrome(r"chromedriver.exe")
browser_2 = webdriver.Chrome(r"chromedriver.exe")
browser.get("https://www.seleniumhq.org/")
browser_2.get("https://www.seleniumhq.org/")
browser.find_element_by_id("menu_download").click()
browser_2.find_element_by_id("menu_download").click()
Is there a cleaner way to click on the download button in both windows or do I have to repeat the code for each instance of the browser?
I am making a program where I need to insert text, tick boxes ect in several instances of the same website and do not want to have to repeat the same code for each window.
Each browser is a different instance. You can not use a part of the code for both. So the answer is no. You cannot use another to do this. Your above code is correct though.
How about this:
path = 'chromedriver.exe'
n_drivers = 2
drivers = [webdriver.Chrome(path) for i in range(n_drivers)]
for driver in drivers:
driver.get('https://www.seleniumhq.org/')
driver.find_element_by_id('menu_download').click()
I am aware that Selenium apparently doesn't support navigating context menus. But I have also seen in several other threads that there is a work-around by using action chains. Using context_click() followed by arrow key commands to navigate through the menus.
All examples I've seen have used Java, and when I translated to Python, only the context_click() command would register. Strangely enough, I wouldn't get an error either. Other sources have said that the context menus Selenium produces are only system level, and thus, Selenium cannot touch them, only create.
So my question is, has anyone been able to successfully navigate and chose options from context menus through Selenium? Python examples are preferred, but I'll take any advice or answers I can get.
Edit:
Code:
driver.get('https://www.google.com/')
actionChains = ActionChains(driver)
actionChains.context_click().send_keys(Keys.ARROW_UP).send_keys(Keys.ENTER).perform()
Context:
This is just a test script that I have been running to test this situation. In my personal project, I need to navigate the context menu to access a chrome extension. Since selenium can only interact within the web page I can't have it click on the button for the Chrome extension that is displayed by the browser. So this is the work-around I have been attempting.
Research:
https://testingrepository.com/how-to-right-click-using-selenium-webdriver/
- This source tells that seleniums context menus are only system level. In Java examples they also use a .build() command. As far as my knowledge, this command is not available to Python.
Select an Option from the Right-Click Menu in Selenium Webdriver - Java
- Thread suggesting that arrow key commands should work. However, all examples use Java and the .build() command as well
https://github.com/SeleniumHQ/selenium/blob/master/py/selenium/webdriver/common/action_chains.py
- shows that ActionChains() are Pythons's version of a .build() command. Might be common knowledge to some. I did not know this prior.
How to perform right click using Selenium ChromeDriver?
- very similar question to mine. While one user suggests that the menu cannot be interacted with, another suggests the actionChains work-around will work.
Thin,
I had the same problem and wonder nobody answered this already... It wasn't possible for me to solve it with selenium, cause selenium would navigate within the page. My solution:
import win32com.client as comclt
wsh= comclt.Dispatch("WScript.Shell")
ActionChains(driver).move_to_element(element).context_click().perform()
wsh.SendKeys("{DOWN}") # send the keys you want
Well we can solve this using selenium along with pyautogui. The reason for using pyautogui is that we need to have control of the mouse for controlling the options on the context menu. To demonstrate this, I am going to use a python code to automatically open a google image of Avengers Endgame in new tab.
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
import pyautogui
URL = 'https://www.google.com/'
PATH = r'C:\Program Files (x86)\chromedriver.exe'
driver = webdriver.Chrome(PATH)
action = ActionChains(driver)
driver.get(URL)
search = driver.find_element_by_name('q')
search.send_keys('Avengers Endgame')
search.send_keys(Keys.RETURN)
image_tab = driver.find_element_by_xpath('//a[text()="Images"]')
image_tab.click()
required_image = driver.find_element_by_xpath('//a[#class="wXeWr islib nfEiy mM5pbd"]')
action.context_click(required_image).perform()
pyautogui.moveTo(120, 130, duration=1)
pyautogui.leftClick()
time.sleep(1)
pyautogui.moveTo(300,40)
pyautogui.leftClick()
Now in the above code, the part till pyautogui.moveTo(120, 130, duration=1) is selenium based. Your answer begins from pyautogui.moveTo(120, 130, duration=1) and what this does is simply moves the mouse button to the open image in new tab option of the context menu(Please note that the screen coordinates may vary based on your screen size). The next line clicks the option(using action.click().perform() won't work as expected).
The next two lines helps in navigating to the tab after it opens. Hope the code helps!
one way to work around this I think at least in java is to perform those actions with one instance of the class Robot.
This is just an example of handling authentication in chromme. Is very usefull when I need to perform actions out the selenium scope.
try{
Robot bot = new Robot();
for(char c: userArray){
bot.keyPress(c);
bot.keyRelease(c);
bot.delay(300);
}
bot.keyPress(KeyEvent.VK_TAB);
bot.keyRelease(KeyEvent.VK_TAB);
for(char c: userArray){
bot.keyPress(c);
bot.keyRelease(c);
bot.delay(300);
}
bot.keyPress(KeyEvent.VK_ENTER);
bot.keyRelease(KeyEvent.VK_ENTER);
}catch(Exception e){
e.printStackTrace();
}
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!