Selenium, Change browser from Chrome to Edge - python

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")

Related

How to change geolocation of chrome selenium driver in Python?

I am trying to trick the chromedriver to make it believe that it is running in a different city. Under normal circumstances, this can easily be done manually as shown in a quick diagram
Then, when a google search is done, the new coordinates are used, and the results that would normally originate from that location are displayed. You can confirm that this worked when you look at the bottom of a Google search page as seen
.
However, Selenium can only control what the browser displays, not the browser in itself. I cannot tell Selenium to automatically click the buttons needed to change the coordinates. I tried the solutions posted here but that is not meant for Python, and even after I tried to adapt the script, nothing seemed to happen.
Is there a browser.execute_script() argument that could work, or is this the wrong way to change the geolocation?
You can do this by importing Selenium DevTools package. Please refer below for complete java code sample:
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
public void geoLocationTest(){
ChromeDriver driver = new ChromeDriver();
Map coordinates = new HashMap()
{{
put("latitude", 50.2334);
put("longitude", 0.2334);
put("accuracy", 1);
}};
driver.executeCdpCommand("Emulation.setGeolocationOverride", coordinates);
driver.get("<your site url>");
}
Reference : Selenium Documentation
Try this code below :
driver.execute_script("window.navigator.geolocation.getCurrentPosition=function(success){"+
"var position = {\"coords\" : {\"latitude\": \"555\",\"longitude\": \"999\"}};"+
"success(position);}");
print(driver.execute_script("var positionStr=\"\";"+
"window.navigator.geolocation.getCurrentPosition(function(pos){positionStr=pos.coords.latitude+\":\"+pos.coords.longitude});"+
"return positionStr;"))
In search for a solution to the same problem I also came across the already postet scripts. While I am yet to find a solution, I assume that the scripts don't work because they do not change the sensors permanently. The sensors are only changed for that one specific call of window.navigator.geolocation.getCurrentPosition.
The website (in this case google) will later call the same function but with the regular (unchanged) geolocation. Happy to hear solutions to permanently change the sensors to then also affect future geolocation requests.
This Can be Done Using Selenium 4.
HashMap<String ,Object> coordinate = new HashMap<String ,Object>();
coordinate.put("latitude", 39.913818);
coordinate.put("longitude", 116.363625);
coordinate.put("accuracy", 1);
((ChromeDriver)driver).executeCdpCommand("Emulation.setGeolocationOverride",coordinate);
driver.navigate().to("URL");

Splinter fill function is very slow, with IE Webdriver

So, I've followed this tutorial to use the Splinter framework with Internet Explorer (https://stirunagari.wordpress.com/2017/08/20/using-internet-explorer-web-driver-with-splinter-framework/), and It's working....well kind of working.
from splinter import Browser
browser = Browser('iexplorer')
browser.visit('http://google.com')
browser.fill('q', 'Text to fill in the search bar')
The search field is being filled but at a very slow rate, like 1 keystroke in 1-2 seconds. While using Chrome or Firefox as the browser the browser.fill is working well.
I know that this issue is most probably because IE is not directly supported by Splinter, but maybe someone knows a workaround or something?
Edit: I don't know what IEDriver I was using before, but I replaced it with IEDriverServer_Win32 from Here,and it's working fine now. I can't answer my question because someone deleted my answer...
I wasn't using the latest Internet Explorer WebDriver; Updated it from here and it's working fine now:
http://selenium-release.storage.googleapis.com/index.html?path=3.8/
Sounds like you are using 32-bit version of IE Driver. You should use 64 bit version of IE driver not sure why but it much more faster.

PhantomJS loads much less HTML than other drivers

I'm trying to load one web page and get some elements from it. So the first thing I do is to check the page using "inspect element". When I search for the tags I'm looking for, I can see them (in Chrome).
But when I try to do driver.get(url) and then driver.find_element_by_..., it doesn't find those elements because they aren't in the source code.
I think that it is probably because it doesn't load the whole page but only a part.
Here is an example:
I'm trying to find ads on the web page.
PREPARED_TABOOLA_BLOCK = """//div[contains(#id,'taboola') and not(ancestor::div[contains(#id,'taboola')])]"""
driver = webdriver.PhantomJS(service_args=["--load-images=false"])
# driver = webdriver.Chrome()
driver.maximize_window()
def find_taboola_blocks_selenium(url):
driver.get(url)
taboola_blocks = driver.find_elements_by_xpath(PREPARED_TABOOLA_BLOCK)
return taboola_blocks
print len(find_taboola_blocks_selenium('http://www.breastfeeding-problems.com/breastfeeding-a-sick-baby.html'))
driver.get('http://www.breastfeeding-problems.com/breastfeeding-a-sick-baby.html')
print len(driver.page_source)
OUTPUTS:
Using PhantomJS:
0
85103
Using ChromeDriver:
3
420869
Do you know how to make PhantomJS to load as much Html as possible or any other way to solve this?
Can you compare the request that ChromeDriver is making versus the request you are making in PhantomJS? Since you are only doing GET for the specified url, you may not be including other request parameters that are needed to get the advertisements.
The open() method may give you a better representation of what you are looking for here: http://phantomjs.org/api/webpage/method/open.html
The reason for this is because PhantomJS, by default, renders in a really small window, which makes it load the mobile version of the site. And with the PhantomJSDriver, calling maximizeWindow() (or maximize_window() in python) does absolutely nothing, since there is no rendered window to maximize. You will have to explicitly set the window's render size with:
edit: Below is the Java solution. I'm not entirely sure what the Python solution would be when setting the window size, but it should be similar.
driver.manage().window().setSize(new Dimension(1920, 1200));
edit again: Found the python version:
driver.set_window_size(1920, 1200)
Hope that helps!
PhantomJS 1.x is a really old browser. It only uses SSLv3 (now disabled on most sites) by default and doesn't implement most cutting edge functionality.
Advertisement scripts are usually delivered over HTTPS (SSLv3/TLS) and usually use some obscure feature of JavaScript which is not well tested or simply not implemented in PhantomJS.
If you use PhantomJS < v1.9.8 then you should use those commandline options (service_args): --ignore-ssl-errors=true --ssl-protocol=any.
If iframes or strange cross-domain requests are necessary for the page/ads to work, then add --web-security=false to the service_args.
If this still doesn't solve the problem, then try upgrading to PhantomJS 2.0.0. You might need to compile it yourself on Linux.

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.

Is it possible to call a Chrome extension's method using Python?

I want to write a Chrome extension which gets the page source and I have found some references (1, 2) on how to do it. However, the end code that would be using this source is in Python. Is there any way I could write Chrome extension and call its methods in Python?
Note:
I have tried using Selenium to get browser's source. However, I'm stuck when the page doesn't stop loading. There is a bug in selenium which prevents it from doing anything if the page doesn't stop loading. The browser doesn't return back to Selenium so I'm trying alternate methods.

Categories

Resources