Default download directory Edge web driver - python - python

I'm trying to change the download location of my web driver for Microsoft Edge but it does not seem to work.
I've tried looking at option for chrome and replicating it for Edge and this is what I got so far
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from msedge.selenium_tools import Edge, EdgeOptions
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
import time
from datetime import datetime
PATH =r"C:\Users\Username\Desktop\test\msedgedriver.exe"
options = EdgeOptions()
options.add_argument(r"download.default_directory=C:\Users\username\Desktop\test")
driver = Edge(PATH)
Please can someone advise?
Thank you.

It seems that your code has some issues. You can refer to the steps below to change the download path when automating Edge with Selenium:
Download the correct version of Edge WebDriver from here. Make sure that the Edge WebDriver version is the same as the Edge browser version.
Install the MS Edge Selenium tools using command below:
pip install msedge-selenium-tools selenium==3.141
Run the following sample python code to test:
from msedge.selenium_tools import Edge, EdgeOptions
options = EdgeOptions()
options.use_chromium = True
options.add_experimental_option("prefs", {
"download.default_directory": r"D:\Downloads"
})
driver = Edge(executable_path=r"D:\webdriver\msedgedriver.exe", options=options)
driver.get("https://www.seleniumhq.org/download/");
m = driver.find_element_by_link_text("32 bit Windows IE")
m.click()
Note: Change the paths in the code to your owns.

This is not a complete answer i.e. you will need to experiment a little because there are effects when setting this. Please also note I have had to type this, as I can't copy and paste, therefore a direct copy and paste from this may not work. These are suggestions:
If I change the download folder as below then I find I get an annoying dialog box saying I am signed into Edge and can sync profile settings, with an OK button (and therefore no automatic way to get rid of the dialog). I can find no way of getting rid of this apart from private browsing. So if I want to use SSO, I don't change the download folder to prevent this, if I am manually logging into a webapp I can change the download folder by using private browsing to avoid the dialog. I have searched and searched and cannot find an answer of setting prefs AND NOT having this dialog.
use Selenium 4. msedge-selenium-tools is deprecated and only for selenium 3.x. selenium 4 supports the settings below. In anaconda be careful that by default conda install selenium will give you selenium 3 by default (unlike pip) so check the selenium docs for the conda command to get 4.
imports (there are many more that are useful)
from selenium import webdriver
from selenium.webdriver.edge.options import Options as EdgeOptions
from selenium.webdriver.edge.service import Service as EdgeService
Other useful imports (see docs) ActionChains, expected_conditions,
WebDriverWait, Keys, By, desired_capabilities,staleness_of and I also use
webelement
To set download directory:
edge_options = EdgeOptions()
edge_options.add_experimental_option("prefs", {"download.default_directory":download_path})
You can add other options e.g.:
edge_options.add_experimental_option("prefs, {
"download.default_directory":download_path,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True
})
There are also command line arguments for certain things, but these seem to take a single value. (I also can't yet tell whether you use a single dash, double dash or no dash as different posts give different answers. Example:
options.add_argument("-inprivate") #private browsing with no windows sign in to edge
Then instantiate the driver. Note that many posts use a now deprecated method of running the driver, you should use service and the options variable and then (example):
myService = EdgeService(executable_path = 'path to driver')
myDriver = webdriver.Edge(service=myService, options=edge_options)
element = myDriver.find_element(By.ID, 'foo') #perform selenium functions

Related

How can I add an extension AND it's settings to chrome driver extensions?

I'm using python, selenium and chromedriver to create a program that fills out a form for me. There is a certain chrome extension I would like to use. The extension I'm talking about is called "Autofill" it has a little lightning bolt picture. I have a "profile" on the extension that fills out a bunch of things on a form at once. I know how to add the extension to chromedriver, but when I do it doesn't save the profile with all the information. I know there are work-arounds to this problem where you can fill it out with commands, but it needs to be very fast and "Autofill" is fast.
We will try to be even more helpful if you share the code sample . As far as I understand; you could use chrome webdriver options like that.
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
executable_path = "path_chromedriver"
os.environ["webdriver.chrome.driver"] = executable_path
chrome_options = Options()
chrome_options.add_extension('path_of_extension')
driver = webdriver.Chrome(executable_path=executable_path, chrome_options=chrome_options)
driver.get("http://google.com")
For more information, you should check https://sites.google.com/a/chromium.org/chromedriver/capabilities

Selenium can't find any elements on IE11 once a dashboard is loaded

So something interesting is happening - unfortunately, I cannot post a url or something you guys can access, so maybe someone just has an idea of what the cause could be. I'd assume it would be something in development. Either way...
I have a test that logs in to an application, which will then load up a dashboard and search for some data. In Firefox, everything works fine. However, IE11 is having an issue once it logs in. So, Selenium works fine up until it loads the dashboard, where it will then find NOTHING. Not even "//html". It gives the basic element not found error. I'm assuming the developers have written something that prevents access, but the one I talked to wasn't sure.
Any idea of what could be causing this so that I could go suggest something to the developers? Again, this doesn't happen in Firefox, which could also suggest something's up with IE11 (though Selenium works fine on other applications and up until the Dashboard screen on this one). If you need more information, let me know - just be aware that I might not have much to give.
A couple things to note:
1. it is https protocol, but so is the login page
2. at the end of the url bar, it says there is a certificate error that will not go away
no matter what I do (I have added the supposedly correct certificates).
What I'm using:
Selenium 2.4.5
32 bit IEDriverServer
Python 2.7.8
IE11 (other IE versions are not supported, so I cannot compare)
Windows 7
EDIT:
I talked with a developer, and the best thing he could think of was the fact that the dashboard heavily uses polyfills, and that maybe that is the reason that Selenium isn't finding anything. With that being said, is there a way to find elements using Selenium when polyfills are involved? Or is Selenium just not going to work for it.
MORE:
Here is some code that equivalent to what my test would do. Assume that every exception would normally be caught and wouldn't kill the script...
from pdb import set_trace
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
url = 'server.url'
capabilities = webdriver.DesiredCapabilities().INTERNETEXPLORER
capabilities['acceptSslCerts'] = True
# use path due to having both a 64 bit and 32 bit version of the driver - specifies which to use
driver = webdriver.Ie("path-to-iedriver.exe", capabilities=capabilities)
driver.maximize_window()
driver.get("https://%s" % url)
# click ie's continue to website
driver.get("javascript:document.getElementById('overridelink').click();")
time.sleep(1)
# clicks 'I Agree' on a page before login
driver.find_element_by_xpath("//button[text()='I Agree']").click()
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'username')))
# logs in
driver.find_element_by_id("username").send_keys("username")
driver.find_element_by_name("j_password").send_keys("password")
driver.find_element_by_xpath("//button[contains(text(), 'Log on')]").click()
# click continue when a successful login box pops up
ret = WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.ID, "continueBtn")))
ret.click()
# this waits for an element on the dashboard page - it will fail
WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.XPATH, "//span[contains(text(), 'banner')]")))
# everything from here on will fail due to element not found (only in IE)
driver.find_element_by_xpath("//html")
driver.find_element_by_xpath("anything")
set_trace()
Again, the elements will be found in Firefox and Chrome, but not IE.
You might need to add an Explicit Wait to wait for the data to be loaded. For example, you may wait for a specific element to become present:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"))
)
# proceed with locating the elements containing the desired data
Old answer
It sounds like this is a certificate issue, print out the .title and the .page_source once the page is loaded and check what is there.
You can also let it accept the certificate by setting acceptSslCerts desired capability to True:
from selenium import webdriver
capabilities = webdriver.DesiredCapabilities().INTERNETEXPLORER
capabilities['acceptSslCerts'] = True
driver = webdriver.Ie(capabilities=capabilities)
# ...
Also see:
How to deal with certificates using Selenium?

searching web-client for python that can handle frames

I'm searching for a python module that simulates a webbrowser and can handle html frames. I want to use the chatbot Brain http://www.thebot.de/ with python. If you know any tutorials that explain how to use your suggested module in relation with forms and frames give me a links to them please.
Tim,
I suggest you take a look at Selenium. By default it opens and manipulates Firefox to navigate the web. Its primary use case is testing, however in a pinch I've used it in some of my scripts to get past sites with a lot of javascript or, as in this case, iframes.
The basic usage is:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
driver.close()
To access an iframe you can use the following code:
elm = driver.find_element_by_tag_name("iframe")
driver.switch_to.frame(elm)
Then when you want to switch out of the iframe:
driver.switch_to.default_content()
You can even utilize Selenium headless with xvfbwrapper like this (code from github.com/cgolberg/xvfbwrapper):
from xvfbwrapper import Xvfb
vdisplay = Xvfb()
vdisplay.start()
# launch stuff inside virtual display here
vdisplay.stop()

configure RefControl extension using selenium webdriver

i am using this addon with selenium and it starts fine, but how do you go about affecting the settings of addons in selenium?
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox import webdriver
profile = FirefoxProfile('/path/to/profile')
driver = webdriver.WebDriver(firefox_profile = profile)
driver.get("http://localhost/referer.html")
driver.find_element_by_link_text("go there").click()
the problem is that this addon does not do anything unless you configure it first, but there is no documentation, that i can find on this.
General way, as alecxe suggested, "to install and configure the extension first, then use that firefox profile with selenium". This will probably work with any extension.
I also search for configuration in prefs.js (or about:conf) and found out, that it stores the value in "refcontrol.actions" preference. This way you will be able to change the extension behavior in your code. Example:
profile = FirefoxProfile('/path/to/profile')
profile.set_preference("refcontrol.actions", '#DEFAULT=#NORMAL example.com=#3RDPARTY:http://www.referer.com/your/referer');

Python/Selenium - Chrome Web Driver, Click Action

I'm having an issue coding a click action using Selenium and the Chrome Web Driver in Python. I've spent some time googling around and found that I have to use another selenium process in order to make a click in Google Chrome, which doesn't make sense to me (Wouldn't it be something while calling webdrive.Chrome?). Though I can't find any other method to make a click, either online or by going through seleniums modules.
Here's what I have, any help is appreciated! Thanks!
EDIT: So I found the ActionChains module in Selenium, can't seem to get this to work either. Updated my code, a bit, still stuck. Does the ChromeDriver really just not support clicks?
import selenium
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time
chromeOps = webdriver.ChromeOptions()
chromeOps._binary_location = "C:\\Applications\\Browser\\Chrome.exe"
chromeOps._arguments = ["--enable-internal-flash"]
browser = webdriver.Chrome("C:\\Applications\\Browser\\chromedriver.exe", port=4445, chrome_options=chromeOps)
time.sleep(3)
browser.get("http://example.com")
##selenium.selenium("127.0.0.1", 4445,'*Chrome.exe', 'https://example.com').click("//a[contains(#href,'http://example.com/link')]")
webdriver.ActionChains(browser).click(on_element='//a[contains(#href,"http://example.com/link")]')
I hate it when such simple things are right infront of you.
clickme = browser.find_element_by_xpath('//a[contains(#href,"http://example.com/link")]')
clickme.click()

Categories

Resources