Selenium webdriver, how to access network tab in inspect element and save data from there - selenium

Please refer to the attached screenshot, I need to save data from 'Save all as HAR' option automatically.

I doubt that selenium provides a way to interact with the inspector. You might have to do something like inject Javascript into the page that re-submits the request and then saves the response/request data on the page in a hidden element. You could then simply get the text from that hidden element via selenium.
Hope that helps

No, Selenium is not the tool you want to use to do this. JMeter may be useful, or this site suggests using a proxy server to do this for you.

Selenium does not give you the ability to track network traffic. It does however allow you to configure a proxy that the browser will use when it is started up.
S to get around your problem you will need to use something like the BrowserMobProxy:
BrowserMobProxy browserMobProxy = new BrowserMobProxyServer();
browserMobProxy.start();
Proxy proxy = ClientUtil.createSeleniumProxy(browserMobProxy, InetAddress.getLoopbackAddress());
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new FirefoxDriver(capabilities);
You can then perform some actions with Selenium and when you want to collect a Har file perform:
browserMobProxy.getHar();
For more information about the BrowserMobProxy have a look at the BrowserMobProxy Homepage

Related

How to change proxy IP of selenium webdriver dynamically?Like some js plugins

Some js plugins can indeed do. How?
js_code = f"""
var prefs = Components.classes["#mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
prefs.setIntPref("network.proxy.type", 1);
prefs.setCharPref("network.proxy.http", "${host}");
prefs.setIntPref("network.proxy.http_port", "${port}");
prefs.setCharPref("network.proxy.ssl", "${host}");
prefs.setIntPref("network.proxy.ssl_port", "${port}");
prefs.setCharPref("network.proxy.ftp", "${host}");
prefs.setIntPref("network.proxy.ftp_port", "${port}");
"""
It is not effict。
I have a more hardcore approach, provided you don't use Headless mode. And they don't care about the speed. You can load a fully fledged proxy plug-in before instantiating WebDriver. LIke SwitchyOmega, when SwitchyOmage is first started, you can choose to go to the second window, and this window is the configuration page of SwitchyOmage, and set its proxy. Then selenium gets the url of the configuration page, closes the tag, and manually launches the plug-in (because Selenium doesn't control it), but later when you want to change the proxy, you can use Selenium to execute JS to open a new tag and go to the URL of the configuration page. Change the proxy IP and port number using selenium control elements. Hahaha, very hardcore!

Is there any option to handle certificate errors for chrome browser?

I am developing my new Automation project, is any any option to handle certificate errors for chrome browser? like we have for selenium -
ChromeOptions opt= new ChromeOptions();
opt.addArguments("ignore-certificate-errors");
When using qaf you can set driver capabilities using properties. You can find details in documentation for setting driver capabilities. When setting capabilities through property, you can provide json representation of capabilities Object. If you don't know how to create json, easiest way is to create capability object and print using JSONUtil.toString(capability) in console, then use it. Below is an example to set to set chrome options:
chrome.additional.capabilities={"chromeOptions":{"args":["--ignore-certificate-errors"]}}
Another way to provide capabilities through code, you can use driver listener you can append capabilities in void beforeInitialize(Capabilities) method
You can also refer similar question.

Can Selenium be used to automate data retrieval from a 3rd party web portal?

I need to automate a manual task to retrieve sales data from a web portal that is not setup for API access.
Can I use a test tool, such as Selenium, to do this task or is there a better solution out there.
I've never used Selenium but it looks easy enough to create record a macro for the button clicks to log in and initiate the download. I also need to trigger the download once a week, notify an email or slack channel if there is an error and then save the file with a specific name including the date.
My hope is that I can do all of this within an test automation tool but willing to explore other options.
Yes it can be implemented. Once you succeed with fetching the data, change your webdriver run option to "Headless", so that Selenium will run at background and will not make the browser visible during runtime. An example for setting headless mode on Firefox:
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.addArguments("--headless")
WebDriver driver = new FirefoxDriver(options); // init driver in headless mode
After that you can use the fetched data in the rest of your program.

I want to know how to open URL in existing browser window in selenium web driver

I am working on selenium web driver. I want to open existing browser window via selenium script(Java).
There is no built-in method in Selenium that supports this feature directly. However, you can try the following:
driver.get("link");
driver.find_element_by_link_text('new link name').click().switch_to.window(driver.window_handles[0]);
Just switch to handle for self.You can also try storing handles in variables if you like.
The whole post can be found here.

Whitelist domains Selenium / Firefox can connect to

I am using Selenium webdriver with firefox. I am wondering if there is a setting i can change such that it to only requesting resources from certain domains. (Specifically i want it only to request content which is on the same domain as the webpage itself).
My current set up, written in Python, is:
from selenium import webdriver
firefox_profile = webdriver.FirefoxProfile()
## Here, I change various default setting in Firefox, and install a couple of monitoring extensions
driver = webdriver.Firefox(firefox_profile)
driver.get(web_address)
What i want to do, is if i specify the web address wwww.domain.com, then to only load content served by domain.com, and not e.g. all the tracking content hosted by other domains that would typically be requested. Hoping could be achieved by a change to the profile settings in firefox, or via an extension.
Note - there is a similar question (without an answer) - Restricting Selenium/Webdriver/HtmlUnit to a certain domain - but it is four years old, and i think Selenium has evolved a lot since then.
With thanks to Vicky, (who's approach of using Proxy settings i followed - although directly from Selenium), the code below will change the proxy settings in firefox such that it will not connect to a domain except that on the white-list.
I suspect several setting changes are unnecessary and can be omitted for most purposes. Code in Python.
from selenium import webdriver
firefox_profile = webdriver.FirefoxProfile()
## replace desired_domain.com below with whitelisted domain. Separate domains by comma.
firefox_profile.set_preference("network.proxy.no_proxies_on","localhost,127.0.0.1,desired_domain.com")
firefox_profile.set_preference("network.proxy.backup.ftp","0.0.0.0")
firefox_profile.set_preference("network.proxy.backup.ftp_port",1)
firefox_profile.set_preference("network.proxy.backup.socks","0.0.0.0")
firefox_profile.set_preference("network.proxy.backup.socks_port",1)
firefox_profile.set_preference("network.proxy.backup.ssl","0.0.0.0")
firefox_profile.set_preference("network.proxy.backup.ssl_port",1)
firefox_profile.set_preference("network.proxy.ftp","0.0.0.0")
firefox_profile.set_preference("network.proxy.ftp_port",1)
firefox_profile.set_preference("network.proxy.http","0.0.0.0")
firefox_profile.set_preference("network.proxy.http_port",1)
firefox_profile.set_preference("network.proxy.socks","0.0.0.0")
firefox_profile.set_preference("network.proxy.socks_port",1)
firefox_profile.set_preference("network.proxy.ssl","0.0.0.0")
firefox_profile.set_preference("network.proxy.ssl_port",1)
firefox_profile.set_preference("network.proxy.type",1)
firefox_profile.set_preference("network.proxy.share_proxy_settings",True)
driver = webdriver.Firefox(firefox_profile)
driver.get(web_address_desired)
I think it is still impossible in selenium.But you can still achieve this by using proxies like browsermob. Webdriver integrates well with browsermob proxy.
Sample pseudeocode in java
//LittleProxy-powered 2.1.0 release
LegacyProxyServer server = new BrowserMobProxyServer();
server.start(0);
// Blacklist websites
server.blacklistRequests("https?://.*\\.blocksite\\.com/.*", 410);//these sites will be blocked
/// get the Selenium proxy object
Proxy proxy = ClientUtil.createSeleniumProxy(server);
// configure it as a desired capability
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, proxy);
// initialize the driver with the capabilities ;
Webdriver driver = new FirefoxDriver(capabilities);
Hope this helps you.Kindly get back if you need any further help