1

I managed to use PowerShell 7.1 and Selenium WebDriver and Module to control the Chrome Browser. I need now to access the network response of a post request which will be invoked after the button is clicked from Selenium.

I found some good info here, here, here, and here , however, it is for Python and Java so I need to convert some code to PowerShell. I hope you can help me out.

The code snippet below from one of the above-mentioned sources is where I am having difficulties with:

...
options = webdriver.ChromeOptions()
options.add_argument("--remote-debugging-port=8000")
driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=options)

dev_tools = pychrome.Browser(url="http://localhost:8000")
tab = dev_tools.list_tab()[0]
tab.start()
...

Specifically, this part dev_tools = pychrome.Browser(url="http://localhost:8000")

Below is another code snippet I got from one of the above-mentioned sources:

ChromeDriver driver = new ChromeDriver();
DevTools devTool = driver.getDevTools()
devTool.createSession();
devTool.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
devTool.addListener(Network.responseReceived(), <lamda-function>)

So it is clear the Selenium 4 has support for DevTools, but I am not finding the main C# documentation so that I can use it with PowerShell.

Finally, after accessing the response, I need to verify it using PowerShell Pester.

I appreciate your help.

tarekahf
  • 738
  • 1
  • 16
  • 42
  • What do you have so far? Do you already have a replacement for `pychrome`? Are you already using something like the `selenium-powershell` module or just straight C#? This might be an example of what you're looking for: https://stackoverflow.com/q/62322625/7411885 – Cpt.Whale Feb 07 '22 at 16:21
  • And see `DevTools` documentation and examples here - select the csharp tab: https://www.selenium.dev/documentation/webdriver/bidirectional/chrome_devtools/ – Cpt.Whale Feb 07 '22 at 16:24
  • Thanks, I am checking the stack overflow link. For the 2nd link, this is my point. The documentation seems to be WIP. I am unable to find a complete page with up-to-date info. Following that link, I was able to land here https://github.com/SeleniumHQ/seleniumhq.github.io/tree/dev/website_and_docs/content/documentation, but not able to find an example from the source about intercepting network post requests. – tarekahf Feb 07 '22 at 17:25
  • I found this link from the dev branch: https://github.com/SeleniumHQ/seleniumhq.github.io/blob/dev/website_and_docs/content/documentation/webdriver/bidirectional/bidi_api.en.md which seems it has an example. But why the page is not rendered properly around the code snippet? – tarekahf Feb 07 '22 at 17:29
  • I looked everywhere here, and didn't find that support for network interception was implemented in C#: https://github.com/SeleniumHQ/selenium/tree/trunk/dotnet/src. I do see that it was implemented in Java only. I think I need to look for the Web Sockets solution instead. – tarekahf Feb 07 '22 at 18:05
  • Actually, after additional round of struggle, I found it. The tests are here: https://github.com/trabasbaeslur/SeleniumHQ/blob/AutomatedTester-codeql/dotnet/test/common/DevTools/DevToolsNetworkTest.cs. The source code is somewhere here: https://github.com/trabasbaeslur/SeleniumHQ/tree/AutomatedTester-codeql/dotnet/src/webdriver/DevTools – tarekahf Feb 07 '22 at 18:55
  • Ops, I just noticed that this project https://github.com/trabasbaeslur/SeleniumHQ is not under the official selenium project. I am lost! – tarekahf Feb 07 '22 at 22:12
  • Are you just having trouble creating the DevTools object? It looks like you can use the logging to check the web requests in a more straightforward way like `$logs = $driver.manage().logs().get("performance")` when you have network logging enabled as detailed here: https://stackoverflow.com/a/39979509/7411885 – Cpt.Whale Feb 09 '22 at 15:56
  • Will this `...logs().get(...)` give me the response of a post request? – tarekahf Feb 09 '22 at 17:55
  • judging from the example in the answer there, the logs should contain detail for each request from chrome to the server (headers, POST data, etc.), and the response code. It's not clear to me if it dumps the entire response though. – Cpt.Whale Feb 09 '22 at 20:30
  • To view the full data sent back from the server (like a web page), the comment says to use `$driver.getPageSource()`, or `$driver.switchTo().frame()`, though this would not be specific to an individual request. It probably depends on what you're looking for. – Cpt.Whale Feb 09 '22 at 20:33
  • I need to intercept a network post request and get the response. – tarekahf Feb 09 '22 at 20:43

0 Answers0