This may sound odd BUT Is it possible to use WebDriver commands on a static variable that holds the html source? (eg: findElementbyId)
This is what i want to do:
set firefox webDriver
open website url
save the HTML pageSource to a "static local variable"
quit webDriver
Now - i want to be able to findElements and texts within this locally stored PageSource. (preferably using the selenium commands)
Any help and/or suggestion is greatly appreciated.
Thanks.
Basically no, it falls down at (5). The FirefoxDriver needs to communicate with an actual Firefox browser using the WebDriver protocol. Selenium can't work with just a String.
It's not clear what your use case is, but you could do things like copy the HTML to a temporary file, generate a file: URL for it, load it with the HtmlUnit or PhantomJS drivers and re-run your tests in-memory.
Surely plain old regular expressions, or an HTML parser like JSoup, are better options for post-processing HTML?
Related
I have a python script which uses Selenium WebDriver to start a Chrome, open an URL, enter simple captcha, check if some data available (time for visit government organization) and repeat this process in 5 minutes.
I want it work in background. Headless mode is not an option because site would show DDos Guard in that case.
I have tried to use driver.minimize_window() but the windows activates for short periods for number input and click of button which is annoying when I work on PC.
Is it possible to make it work completely in background without activate Chrome window?
You can packaging your script using PyInstaller, then run it with Windows Task Scheduler (assuming you are using Windows)
Install pyinstaller
pip install -U pyinstaller
2. Packaging your script (run in cmd)
pyinstaller --onefile --name=your-package-name yourscript.py
Check pyinstaller docs and make sure add --hidden-import or --collect-data flag if needed.
Setup your task scheduler:
Create basic task, choose task name, and description (if needed)
Create trigger
Set action to start a program then define your script name in Program/script and make sure put your script folder path in Start in
Then voila. You can custom the trigger more detail in task properties (creating multiple triggers, specific conditions, etc...)
You could try to start your exe file as process manually using cmd/powershell like this SO answer, and some kind of process/service manager but I have never tried it yet.
When I use selenium and headless option don't work with me, I always do this trick by changing the window size and just leave it.
So, set the window size in the driver itself, like this:
driver.set_window_size(1, 1)
Or, set it as an argument, like this:
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('window-size=1,1')
Or use both.
I hope this helps you.
I am playing with CefGlue (the .net wrapper around chromium embedded") I need to make some proof of concept so I create my own special html files that contains what I need to test. However I can't find a way to load those files.
The CefBrowserHost.CreateBrowser(cefWindowInfo, cefClient, cefBrowserSettings, url); requires an url and there is no overloaded method that accepts the content as string. So the question is: How to load html file from disk?
I've not used CefGlue - but in general you can use a custom cef resource handler to load the file. Briefly, when CEF sees your URL, they will call through a resource override handler you set up, which will then read that resource into a byte stream. In our case we read an html file compiled into our application resources. In your case I suppose you could also read the resource from disk at that time, though I've not done this. If you can compile resources into your C# app, you can add the html file into the Resources if you prefer.
We pass the url to CreateBrowserSync() in our case, and CEF ends up calling our ResourceHandler to load it. The CefClient c++ sample has an excellent example of this, see resource_util_win.cpp.
This is set up in a Handler override in CefResourceHandler. We overrode GetResourceHandler, their example overrides ProcessRequest in SchemeHandler. See scheme_test.cpp in the CefClient sample.
Most of the code from their samples is pretty boiler plate and you should be able to use what they have as the basis for your implementation - it's really too much code to list it all out here though.
I found a solution: just pass the full path to the file in the url parameter and everything works fine. It's just like chrome opening file from disk so I don't know why I did not tried that the first time.
Every time I run my test selenium opens another Firefox window. It's annoying to see so many windows after several runs of the test. Is it possible to utilize the previous opened one?
For Selenium 2 (WebDriver), try creating a new object for FF:
ffWebDriver = new FirefoxDriver();
This should create an object of the Firefox web-driver that you can control and won't be quit until you call:
ffWebDriver.Quit();
This is something NOT possible. Please look into https://code.google.com/p/selenium/issues/detail?id=18 to have an idea why is it so. If only multiple windows opened is a problem here, you can quit all the browser instances opened after each test by calling driver.quit(); method.
In case you are using Python bindings, you might want to have a look into this : http://webdriverplus.org/en/latest/browsers.html#reuse-browser
How to download any file using selenium webdriver.what is the logic to download any file in selenium webdriver
If you mean "any" file that browser would show, i.e. any html file, you just need to call
driver.getPageSource();
If you mean "any" file as in the "save link as" or similar menu of browsers you are out of luck, since this triggers the OS-driven download file chooser of a browser which can't be controlled by Selenium webdriver.
Solution to 2.
You have two options here I think:
a) use something like AutoIT ontop of selenium. This becomes very hard to control in a short time, is not portable and will make your tests error prone.
b) The better solution is probably to download the file outside of selenium. I found a nice article describing the whole dilemma here. It also contains a nice solution to the problem that takes even care of cookie-handling if needed for the download.
Actually it's not a good idea to download files using web-driver. I don't think that you need the file; in most cases, it's just for testing download links and for this purpose you can use driver.find_element_by_tag_name("a"), driver.find_elements_by_link_text("some text") or driver.find_elements_by_partial_link_text("a"); after finding the element, you just click() on it and check if the response's URL is actually what it should be.
Still if you have a goal for completely downloading the file, I'd be glad if you let me know.
EDIT:
Today, I visit this page and I think this will help you, specially this comment.
how can I get size of a specific file by ExtJS, without using ActiveXObject (as recommended at some website), because my program just only run on Firefox?
Thank you so much!
(More detail: the file I want to get size in on my local, not on remote server)
Your code runs in a sandbox, without extensions like ActiveX you are not allowed access to the file system.
You can use a Flash object. Have a look into SWFUload