Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[🐛 Bug]: RequestWillBeSent doesn't work when the link opens another tab #10161

Open
lopukhDA opened this issue Dec 17, 2021 · 14 comments
Open
Assignees

Comments

@lopukhDA
Copy link

lopukhDA commented Dec 17, 2021

What happened?

Hello. I am using selenium to check submitted Requests from a page. I use the Network.RequestWillBeSent event for this. Everything works well until I come across a link that opens a new tab. Such links usually have a target = "_blank" attribute. When a new tab opens, I do not receive requests from it. I can only get requests from the initial tab.

How can I get all the requests from the new tab? If I need to initialize a new RequestWillBeSent event for a new tab, then I will miss the first requests from the page, which are very important to me.

I was trying to switch to another tab using driver.SwitchTo().Window(window), but that doesn't work.

How can we reproduce the issue?

static async Task Main(string[] args)
        {
            var options = new ChromeOptions();

            var driver = new ChromeDriver(ChromeDriverService.CreateDefaultService(), options, TimeSpan.FromMinutes(2));

            var devTools = (IDevTools)driver;
            var session = devTools.GetDevToolsSession();
            var domains = session.GetVersionSpecificDomains<DevToolsSessionDomains>();

            void RequestWillBeSentHandler(object sender, RequestWillBeSentEventArgs e)
            {
                Console.WriteLine(e.Request.Url);
            }

            domains.Network.RequestWillBeSent += RequestWillBeSentHandler;
            await domains.Network.Enable(new EnableCommandSettings());

            driver.Navigate().GoToUrl("https://www.selenium.dev/");
            driver.FindElement(By.XPath("//a[@aria-label='GitHub']")).Click();
            Console.WriteLine("------------------------------------------------");
            driver.Navigate().Refresh();

            driver.Quit();
        }

Relevant log output

Starting ChromeDriver 96.0.4664.45 (76e4c1bb2ab4671b8beba3444e61c0f17584b2fc-refs/branch-heads/4664@{#947}) on port 50285
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.

DevTools listening on ws://127.0.0.1:50288/devtools/browser/fc80388a-ede6-4be6-8b6a-36242934acee
[37120:3384:1217/105342.756:ERROR:chrome_browser_main_extra_parts_metrics.cc(226)] crbug.com/1216328: Checking Bluetooth availability started. Please report if there is no report that this ends.
[37120:3384:1217/105342.757:ERROR:chrome_browser_main_extra_parts_metrics.cc(229)] crbug.com/1216328: Checking Bluetooth availability ended.
[37120:3384:1217/105342.757:ERROR:chrome_browser_main_extra_parts_metrics.cc(232)] crbug.com/1216328: Checking default browser status started. Please report if there is no report that this ends.
[37120:3384:1217/105342.762:ERROR:chrome_browser_main_extra_parts_metrics.cc(236)] crbug.com/1216328: Checking default browser status ended.
https://www.selenium.dev/
https://www.selenium.dev/scss/main.min.9dfeff3fd02d7e6770041efaeaa2d7e9ecd602e820f926eb6921691c1f253d02.css
https://code.jquery.com/jquery-3.5.1.min.js
https://www.selenium.dev/css/prism.css
https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.css
https://plausible.io/js/plausible.js
https://www.selenium.dev/images/sponsors/browserstack.png
https://www.selenium.dev/images/sponsors/saucelabs.png
https://www.selenium.dev/images/sponsors/experitest.png
https://www.selenium.dev/images/sponsors/applitools.png
https://www.selenium.dev/images/sponsors/perfecto-perforce.jpg
https://www.selenium.dev/images/sponsors/lambda-test.png
https://www.selenium.dev/images/sponsors/subject7_logo.png
https://www.selenium.dev/images/sponsors/Avo_logo.png
https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif
https://www.paypal.com/en_US/i/scr/pixel.gif
https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js
https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.min.js
https://www.selenium.dev/js/tabpane-persist.js
https://www.selenium.dev/js/main.min.5c74b870c6953931a705f390a49c7e4c0a842ec5c83b24354758dd674343ed0d.js
https://www.selenium.dev/js/prism.js
https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.js
https://fonts.googleapis.com/css?family=Encode+Sans:300,300i,400,400i,700,700i&display=swap
https://cdn.jsdelivr.net/gh/rastikerdar/vazir-font@v27.0.1/dist/font-face.css
https://fonts.googleapis.com/css2?family=Rubik:wght@300;400;500;600;700&display=swap
https://fonts.googleapis.com/css2?family=Tajawal:wght@300;400;500;700&display=swap
data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100%25' height='100%25' viewBox='0 0 1600 800'%3E%3Cg %3E%3Cpath fill='%2375c563' d='M486 705.8c-109.3-21.8-223.4-32.2-335.3-19.4C99.5 692.1 49 703 0 719.8V800h843.8c-115.9-33.2-230.8-68.1-347.6-92.2C492.8 707.1 489.4 706.5 486 705.8z'/%3E%3Cpath fill='%2370c25c' d='M1600 0H0v719.8c49-16.8 99.5-27.8 150.7-33.5c111.9-12.7 226-2.4 335.3 19.4c3.4 0.7 6.8 1.4 10.2 2c116.8 24 231.7 59 347.6 92.2H1600V0z'/%3E%3Cpath fill='%236ac056' d='M478.4 581c3.2 0.8 6.4 1.7 9.5 2.5c196.2 52.5 388.7 133.5 593.5 176.6c174.2 36.6 349.5 29.2 518.6-10.2V0H0v574.9c52.3-17.6 106.5-27.7 161.1-30.9C268.4 537.4 375.7 554.2 478.4 581z'/%3E%3Cpath fill='%2365bd4f' d='M0 0v429.4c55.6-18.4 113.5-27.3 171.4-27.7c102.8-0.8 203.2 22.7 299.3 54.5c3 1 5.9 2 8.9 3c183.6 62 365.7 146.1 562.4 192.1c186.7 43.7 376.3 34.4 557.9-12.6V0H0z'/%3E%3Cpath fill='%235fbb49' d='M181.8 259.4c98.2 6 191.9 35.2 281.3 72.1c2.8 1.1 5.5 2.3 8.3 3.4c171 71.6 342.7 158.5 531.3 207.7c198.8 51.8 403.4 40.8 597.3-14.8V0H0v283.2C59 263.6 120.6 255.7 181.8 259.4z'/%3E%3Cpath fill='%2359b943' d='M1600 0H0v136.3c62.3-20.9 127.7-27.5 192.2-19.2c93.6 12.1 180.5 47.7 263.3 89.6c2.6 1.3 5.1 2.6 7.7 3.9c158.4 81.1 319.7 170.9 500.3 223.2c210.5 61 430.8 49 636.6-16.6V0z'/%3E%3Cpath fill='%2354b73d' d='M454.9 86.3C600.7 177 751.6 269.3 924.1 325c208.6 67.4 431.3 60.8 637.9-5.3c12.8-4.1 25.4-8.4 38.1-12.9V0H288.1c56 21.3 108.7 50.6 159.7 82C450.2 83.4 452.5 84.9 454.9 86.3z'/%3E%3Cpath fill='%234eb436' d='M1600 0H498c118.1 85.8 243.5 164.5 386.8 216.2c191.8 69.2 400 74.7 595 21.1c40.8-11.2 81.1-25.2 120.3-41.7V0z'/%3E%3Cpath fill='%2349b230' d='M1397.5 154.8c47.2-10.6 93.6-25.3 138.6-43.8c21.7-8.9 43-18.8 63.9-29.5V0H643.4c62.9 41.7 129.7 78.2 202.1 107.4C1020.4 178.1 1214.2 196.1 1397.5 154.8z'/%3E%3Cpath fill='%2343b02a' d='M1315.3 72.4c75.3-12.6 148.9-37.1 216.8-72.4h-723C966.8 71 1144.7 101 1315.3 72.4z'/%3E%3C/g%3E%3C/svg%3E
data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100%25' height='100%25' viewBox='0 0 1600 800'%3E%3Cg %3E%3Cpath fill='%2385d8ee' d='M486 705.8c-109.3-21.8-223.4-32.2-335.3-19.4C99.5 692.1 49 703 0 719.8V800h843.8c-115.9-33.2-230.8-68.1-347.6-92.2C492.8 707.1 489.4 706.5 486 705.8z'/%3E%3Cpath fill='%2380d7ed' d='M1600 0H0v719.8c49-16.8 99.5-27.8 150.7-33.5c111.9-12.7 226-2.4 335.3 19.4c3.4 0.7 6.8 1.4 10.2 2c116.8 24 231.7 59 347.6 92.2H1600V0z'/%3E%3Cpath fill='%237bd5ed' d='M478.4 581c3.2 0.8 6.4 1.7 9.5 2.5c196.2 52.5 388.7 133.5 593.5 176.6c174.2 36.6 349.5 29.2 518.6-10.2V0H0v574.9c52.3-17.6 106.5-27.7 161.1-30.9C268.4 537.4 375.7 554.2 478.4 581z'/%3E%3Cpath fill='%2376d4ec' d='M0 0v429.4c55.6-18.4 113.5-27.3 171.4-27.7c102.8-0.8 203.2 22.7 299.3 54.5c3 1 5.9 2 8.9 3c183.6 62 365.7 146.1 562.4 192.1c186.7 43.7 376.3 34.4 557.9-12.6V0H0z'/%3E%3Cpath fill='%2371d2eb' d='M181.8 259.4c98.2 6 191.9 35.2 281.3 72.1c2.8 1.1 5.5 2.3 8.3 3.4c171 71.6 342.7 158.5 531.3 207.7c198.8 51.8 403.4 40.8 597.3-14.8V0H0v283.2C59 263.6 120.6 255.7 181.8 259.4z'/%3E%3Cpath fill='%236dd1ea' d='M1600 0H0v136.3c62.3-20.9 127.7-27.5 192.2-19.2c93.6 12.1 180.5 47.7 263.3 89.6c2.6 1.3 5.1 2.6 7.7 3.9c158.4 81.1 319.7 170.9 500.3 223.2c210.5 61 430.8 49 636.6-16.6V0z'/%3E%3Cpath fill='%2368cfea' d='M454.9 86.3C600.7 177 751.6 269.3 924.1 325c208.6 67.4 431.3 60.8 637.9-5.3c12.8-4.1 25.4-8.4 38.1-12.9V0H288.1c56 21.3 108.7 50.6 159.7 82C450.2 83.4 452.5 84.9 454.9 86.3z'/%3E%3Cpath fill='%2363cee9' d='M1600 0H498c118.1 85.8 243.5 164.5 386.8 216.2c191.8 69.2 400 74.7 595 21.1c40.8-11.2 81.1-25.2 120.3-41.7V0z'/%3E%3Cpath fill='%235ecce9' d='M1397.5 154.8c47.2-10.6 93.6-25.3 138.6-43.8c21.7-8.9 43-18.8 63.9-29.5V0H643.4c62.9 41.7 129.7 78.2 202.1 107.4C1020.4 178.1 1214.2 196.1 1397.5 154.8z'/%3E%3Cpath fill='%2359cbe8' d='M1315.3 72.4c75.3-12.6 148.9-37.1 216.8-72.4h-723C966.8 71 1144.7 101 1315.3 72.4z'/%3E%3C/g%3E%3C/svg%3E
data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100%25' height='100%25' viewBox='0 0 1600 800'%3E%3Cg %3E%3Cpath fill='%234669b9' d='M486 705.8c-109.3-21.8-223.4-32.2-335.3-19.4C99.5 692.1 49 703 0 719.8V800h843.8c-115.9-33.2-230.8-68.1-347.6-92.2C492.8 707.1 489.4 706.5 486 705.8z'/%3E%3Cpath fill='%233f63b6' d='M1600 0H0v719.8c49-16.8 99.5-27.8 150.7-33.5c111.9-12.7 226-2.4 335.3 19.4c3.4 0.7 6.8 1.4 10.2 2c116.8 24 231.7 59 347.6 92.2H1600V0z'/%3E%3Cpath fill='%23385db4' d='M478.4 581c3.2 0.8 6.4 1.7 9.5 2.5c196.2 52.5 388.7 133.5 593.5 176.6c174.2 36.6 349.5 29.2 518.6-10.2V0H0v574.9c52.3-17.6 106.5-27.7 161.1-30.9C268.4 537.4 375.7 554.2 478.4 581z'/%3E%3Cpath fill='%232f57b1' d='M0 0v429.4c55.6-18.4 113.5-27.3 171.4-27.7c102.8-0.8 203.2 22.7 299.3 54.5c3 1 5.9 2 8.9 3c183.6 62 365.7 146.1 562.4 192.1c186.7 43.7 376.3 34.4 557.9-12.6V0H0z'/%3E%3Cpath fill='%232651ae' d='M181.8 259.4c98.2 6 191.9 35.2 281.3 72.1c2.8 1.1 5.5 2.3 8.3 3.4c171 71.6 342.7 158.5 531.3 207.7c198.8 51.8 403.4 40.8 597.3-14.8V0H0v283.2C59 263.6 120.6 255.7 181.8 259.4z'/%3E%3Cpath fill='%23214bab' d='M1600 0H0v136.3c62.3-20.9 127.7-27.5 192.2-19.2c93.6 12.1 180.5 47.7 263.3 89.6c2.6 1.3 5.1 2.6 7.7 3.9c158.4 81.1 319.7 170.9 500.3 223.2c210.5 61 430.8 49 636.6-16.6V0z'/%3E%3Cpath fill='%231c45a8' d='M454.9 86.3C600.7 177 751.6 269.3 924.1 325c208.6 67.4 431.3 60.8 637.9-5.3c12.8-4.1 25.4-8.4 38.1-12.9V0H288.1c56 21.3 108.7 50.6 159.7 82C450.2 83.4 452.5 84.9 454.9 86.3z'/%3E%3Cpath fill='%23153fa6' d='M1600 0H498c118.1 85.8 243.5 164.5 386.8 216.2c191.8 69.2 400 74.7 595 21.1c40.8-11.2 81.1-25.2 120.3-41.7V0z'/%3E%3Cpath fill='%230c39a3' d='M1397.5 154.8c47.2-10.6 93.6-25.3 138.6-43.8c21.7-8.9 43-18.8 63.9-29.5V0H643.4c62.9 41.7 129.7 78.2 202.1 107.4C1020.4 178.1 1214.2 196.1 1397.5 154.8z'/%3E%3Cpath fill='%230033a0' d='M1315.3 72.4c75.3-12.6 148.9-37.1 216.8-72.4h-723C966.8 71 1144.7 101 1315.3 72.4z'/%3E%3C/g%3E%3C/svg%3E
https://fonts.gstatic.com/s/encodesans/v8/LDIhapOFNxEwR-Bd1O9uYNmnUQomAgE25imKSbHLSMA6.woff2
https://www.selenium.dev/webfonts/fa-solid-900.woff2
https://www.selenium.dev/webfonts/fa-brands-400.woff2
https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif
https://www.paypalobjects.com/en_US/i/scr/pixel.gif
https://www.selenium.dev/favicons/favicon.ico
------------------------------------------------
https://www.selenium.dev/favicons/favicon-32x32.png
https://www.selenium.dev/
https://www.selenium.dev/scss/main.min.9dfeff3fd02d7e6770041efaeaa2d7e9ecd602e820f926eb6921691c1f253d02.css
https://code.jquery.com/jquery-3.5.1.min.js
https://www.selenium.dev/css/prism.css
https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.css
https://plausible.io/js/plausible.js
https://www.selenium.dev/images/sponsors/browserstack.png
https://www.selenium.dev/images/sponsors/saucelabs.png
https://www.selenium.dev/images/sponsors/experitest.png
https://www.selenium.dev/images/sponsors/applitools.png
https://www.selenium.dev/images/sponsors/perfecto-perforce.jpg
https://www.selenium.dev/images/sponsors/lambda-test.png
https://www.selenium.dev/images/sponsors/subject7_logo.png
https://www.selenium.dev/images/sponsors/Avo_logo.png
https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif
https://www.paypal.com/en_US/i/scr/pixel.gif
https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js
https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.min.js
https://www.selenium.dev/js/tabpane-persist.js
https://www.selenium.dev/js/main.min.5c74b870c6953931a705f390a49c7e4c0a842ec5c83b24354758dd674343ed0d.js
https://www.selenium.dev/js/prism.js
https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.js
https://fonts.googleapis.com/css?family=Encode+Sans:300,300i,400,400i,700,700i&display=swap
https://cdn.jsdelivr.net/gh/rastikerdar/vazir-font@v27.0.1/dist/font-face.css
https://fonts.googleapis.com/css2?family=Rubik:wght@300;400;500;600;700&display=swap
https://fonts.googleapis.com/css2?family=Tajawal:wght@300;400;500;700&display=swap
https://fonts.gstatic.com/s/encodesans/v8/LDIhapOFNxEwR-Bd1O9uYNmnUQomAgE25imKSbHLSMA6.woff2
https://www.selenium.dev/webfonts/fa-solid-900.woff2
https://www.selenium.dev/webfonts/fa-brands-400.woff2
data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100%25' height='100%25' viewBox='0 0 1600 800'%3E%3Cg %3E%3Cpath fill='%2375c563' d='M486 705.8c-109.3-21.8-223.4-32.2-335.3-19.4C99.5 692.1 49 703 0 719.8V800h843.8c-115.9-33.2-230.8-68.1-347.6-92.2C492.8 707.1 489.4 706.5 486 705.8z'/%3E%3Cpath fill='%2370c25c' d='M1600 0H0v719.8c49-16.8 99.5-27.8 150.7-33.5c111.9-12.7 226-2.4 335.3 19.4c3.4 0.7 6.8 1.4 10.2 2c116.8 24 231.7 59 347.6 92.2H1600V0z'/%3E%3Cpath fill='%236ac056' d='M478.4 581c3.2 0.8 6.4 1.7 9.5 2.5c196.2 52.5 388.7 133.5 593.5 176.6c174.2 36.6 349.5 29.2 518.6-10.2V0H0v574.9c52.3-17.6 106.5-27.7 161.1-30.9C268.4 537.4 375.7 554.2 478.4 581z'/%3E%3Cpath fill='%2365bd4f' d='M0 0v429.4c55.6-18.4 113.5-27.3 171.4-27.7c102.8-0.8 203.2 22.7 299.3 54.5c3 1 5.9 2 8.9 3c183.6 62 365.7 146.1 562.4 192.1c186.7 43.7 376.3 34.4 557.9-12.6V0H0z'/%3E%3Cpath fill='%235fbb49' d='M181.8 259.4c98.2 6 191.9 35.2 281.3 72.1c2.8 1.1 5.5 2.3 8.3 3.4c171 71.6 342.7 158.5 531.3 207.7c198.8 51.8 403.4 40.8 597.3-14.8V0H0v283.2C59 263.6 120.6 255.7 181.8 259.4z'/%3E%3Cpath fill='%2359b943' d='M1600 0H0v136.3c62.3-20.9 127.7-27.5 192.2-19.2c93.6 12.1 180.5 47.7 263.3 89.6c2.6 1.3 5.1 2.6 7.7 3.9c158.4 81.1 319.7 170.9 500.3 223.2c210.5 61 430.8 49 636.6-16.6V0z'/%3E%3Cpath fill='%2354b73d' d='M454.9 86.3C600.7 177 751.6 269.3 924.1 325c208.6 67.4 431.3 60.8 637.9-5.3c12.8-4.1 25.4-8.4 38.1-12.9V0H288.1c56 21.3 108.7 50.6 159.7 82C450.2 83.4 452.5 84.9 454.9 86.3z'/%3E%3Cpath fill='%234eb436' d='M1600 0H498c118.1 85.8 243.5 164.5 386.8 216.2c191.8 69.2 400 74.7 595 21.1c40.8-11.2 81.1-25.2 120.3-41.7V0z'/%3E%3Cpath fill='%2349b230' d='M1397.5 154.8c47.2-10.6 93.6-25.3 138.6-43.8c21.7-8.9 43-18.8 63.9-29.5V0H643.4c62.9 41.7 129.7 78.2 202.1 107.4C1020.4 178.1 1214.2 196.1 1397.5 154.8z'/%3E%3Cpath fill='%2343b02a' d='M1315.3 72.4c75.3-12.6 148.9-37.1 216.8-72.4h-723C966.8 71 1144.7 101 1315.3 72.4z'/%3E%3C/g%3E%3C/svg%3E
data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100%25' height='100%25' viewBox='0 0 1600 800'%3E%3Cg %3E%3Cpath fill='%2385d8ee' d='M486 705.8c-109.3-21.8-223.4-32.2-335.3-19.4C99.5 692.1 49 703 0 719.8V800h843.8c-115.9-33.2-230.8-68.1-347.6-92.2C492.8 707.1 489.4 706.5 486 705.8z'/%3E%3Cpath fill='%2380d7ed' d='M1600 0H0v719.8c49-16.8 99.5-27.8 150.7-33.5c111.9-12.7 226-2.4 335.3 19.4c3.4 0.7 6.8 1.4 10.2 2c116.8 24 231.7 59 347.6 92.2H1600V0z'/%3E%3Cpath fill='%237bd5ed' d='M478.4 581c3.2 0.8 6.4 1.7 9.5 2.5c196.2 52.5 388.7 133.5 593.5 176.6c174.2 36.6 349.5 29.2 518.6-10.2V0H0v574.9c52.3-17.6 106.5-27.7 161.1-30.9C268.4 537.4 375.7 554.2 478.4 581z'/%3E%3Cpath fill='%2376d4ec' d='M0 0v429.4c55.6-18.4 113.5-27.3 171.4-27.7c102.8-0.8 203.2 22.7 299.3 54.5c3 1 5.9 2 8.9 3c183.6 62 365.7 146.1 562.4 192.1c186.7 43.7 376.3 34.4 557.9-12.6V0H0z'/%3E%3Cpath fill='%2371d2eb' d='M181.8 259.4c98.2 6 191.9 35.2 281.3 72.1c2.8 1.1 5.5 2.3 8.3 3.4c171 71.6 342.7 158.5 531.3 207.7c198.8 51.8 403.4 40.8 597.3-14.8V0H0v283.2C59 263.6 120.6 255.7 181.8 259.4z'/%3E%3Cpath fill='%236dd1ea' d='M1600 0H0v136.3c62.3-20.9 127.7-27.5 192.2-19.2c93.6 12.1 180.5 47.7 263.3 89.6c2.6 1.3 5.1 2.6 7.7 3.9c158.4 81.1 319.7 170.9 500.3 223.2c210.5 61 430.8 49 636.6-16.6V0z'/%3E%3Cpath fill='%2368cfea' d='M454.9 86.3C600.7 177 751.6 269.3 924.1 325c208.6 67.4 431.3 60.8 637.9-5.3c12.8-4.1 25.4-8.4 38.1-12.9V0H288.1c56 21.3 108.7 50.6 159.7 82C450.2 83.4 452.5 84.9 454.9 86.3z'/%3E%3Cpath fill='%2363cee9' d='M1600 0H498c118.1 85.8 243.5 164.5 386.8 216.2c191.8 69.2 400 74.7 595 21.1c40.8-11.2 81.1-25.2 120.3-41.7V0z'/%3E%3Cpath fill='%235ecce9' d='M1397.5 154.8c47.2-10.6 93.6-25.3 138.6-43.8c21.7-8.9 43-18.8 63.9-29.5V0H643.4c62.9 41.7 129.7 78.2 202.1 107.4C1020.4 178.1 1214.2 196.1 1397.5 154.8z'/%3E%3Cpath fill='%2359cbe8' d='M1315.3 72.4c75.3-12.6 148.9-37.1 216.8-72.4h-723C966.8 71 1144.7 101 1315.3 72.4z'/%3E%3C/g%3E%3C/svg%3E
data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100%25' height='100%25' viewBox='0 0 1600 800'%3E%3Cg %3E%3Cpath fill='%234669b9' d='M486 705.8c-109.3-21.8-223.4-32.2-335.3-19.4C99.5 692.1 49 703 0 719.8V800h843.8c-115.9-33.2-230.8-68.1-347.6-92.2C492.8 707.1 489.4 706.5 486 705.8z'/%3E%3Cpath fill='%233f63b6' d='M1600 0H0v719.8c49-16.8 99.5-27.8 150.7-33.5c111.9-12.7 226-2.4 335.3 19.4c3.4 0.7 6.8 1.4 10.2 2c116.8 24 231.7 59 347.6 92.2H1600V0z'/%3E%3Cpath fill='%23385db4' d='M478.4 581c3.2 0.8 6.4 1.7 9.5 2.5c196.2 52.5 388.7 133.5 593.5 176.6c174.2 36.6 349.5 29.2 518.6-10.2V0H0v574.9c52.3-17.6 106.5-27.7 161.1-30.9C268.4 537.4 375.7 554.2 478.4 581z'/%3E%3Cpath fill='%232f57b1' d='M0 0v429.4c55.6-18.4 113.5-27.3 171.4-27.7c102.8-0.8 203.2 22.7 299.3 54.5c3 1 5.9 2 8.9 3c183.6 62 365.7 146.1 562.4 192.1c186.7 43.7 376.3 34.4 557.9-12.6V0H0z'/%3E%3Cpath fill='%232651ae' d='M181.8 259.4c98.2 6 191.9 35.2 281.3 72.1c2.8 1.1 5.5 2.3 8.3 3.4c171 71.6 342.7 158.5 531.3 207.7c198.8 51.8 403.4 40.8 597.3-14.8V0H0v283.2C59 263.6 120.6 255.7 181.8 259.4z'/%3E%3Cpath fill='%23214bab' d='M1600 0H0v136.3c62.3-20.9 127.7-27.5 192.2-19.2c93.6 12.1 180.5 47.7 263.3 89.6c2.6 1.3 5.1 2.6 7.7 3.9c158.4 81.1 319.7 170.9 500.3 223.2c210.5 61 430.8 49 636.6-16.6V0z'/%3E%3Cpath fill='%231c45a8' d='M454.9 86.3C600.7 177 751.6 269.3 924.1 325c208.6 67.4 431.3 60.8 637.9-5.3c12.8-4.1 25.4-8.4 38.1-12.9V0H288.1c56 21.3 108.7 50.6 159.7 82C450.2 83.4 452.5 84.9 454.9 86.3z'/%3E%3Cpath fill='%23153fa6' d='M1600 0H498c118.1 85.8 243.5 164.5 386.8 216.2c191.8 69.2 400 74.7 595 21.1c40.8-11.2 81.1-25.2 120.3-41.7V0z'/%3E%3Cpath fill='%230c39a3' d='M1397.5 154.8c47.2-10.6 93.6-25.3 138.6-43.8c21.7-8.9 43-18.8 63.9-29.5V0H643.4c62.9 41.7 129.7 78.2 202.1 107.4C1020.4 178.1 1214.2 196.1 1397.5 154.8z'/%3E%3Cpath fill='%230033a0' d='M1315.3 72.4c75.3-12.6 148.9-37.1 216.8-72.4h-723C966.8 71 1144.7 101 1315.3 72.4z'/%3E%3C/g%3E%3C/svg%3E
https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif
https://www.paypalobjects.com/en_US/i/scr/pixel.gif
https://www.selenium.dev/favicons/favicon.ico

C:\Users\ABC\source\repos\ConsoleApp3\ConsoleApp3\bin\Debug\net5.0\ConsoleApp3.exe (process 32436) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

Operating System

Windows 10

Selenium version

Selenium 4.1, .Net Core 5 or .NET Framework 4.7.2(C# 8)

What are the browser(s) and version(s) where you see this issue?

Chrome 96

What are the browser driver(s) and version(s) where you see this issue?

ChromeDriver 96

Are you using Selenium Grid?

no

@github-actions
Copy link

@lopukhDA, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

@daniel-jong
Copy link

I also ran into this, and have tried multiple things that so far have not resulted into something I consider a decent solution.
Like you I'm trying to record 'Network.RequestWillBeSent' events from the moment a new tab is opened by selenium clicking a button.

So far I have figured out that when working with multiple tabs in DevTools you have to use the Target domain to subscribe to TargetCreation (think Browser tabs, Service workers etc). However the SetAutoAttach command that should be used does not seem to do anything. I'm able to receive Target.TargetCreated events but no Target.AttachedToTarget events, unless I manually attach newly created targets.

This manually attaching of a Target causes a new devtools sessions to be created. Which in itself is an issue because the DevToolsSession exposed by selenium only works on a single ActiveSessionId.

After some messing around I have this janky setup where I'm able to activate the network domain for the new session targeting said new tab, by using reflection to change the ActiveSessionId.
This causes (most of) the network events from a new tab to appear. But is a mayor concern due to it's race conditions and other weird behavior. Again I do not recommend this as a solution! https://gist.github.com/daniel-jong/40ea513b1da95298b8f592d4b7490950

I would summarize the issues that I'm running into when working in multiple tabs as following:

  • The Devtools methods SetAutoAttach does not seem to attach new tabs that where created by clicks using Selenium.
  • Manualy attaching to new targets does not allow for the option 'WaitForDebuggerOnStart' causing some network events to be missed, because the tab starts loading before it can be attached.
  • Manualy attaching to new targets introduces extra DevTools sessions that are not controlable in a clean way.
  • Like you have said. The window targeted by driver.SwitchTo().Window(..) and the one targeted by a DevTools session are not in sync.

At this point I'm consider myself to be stuck and could use some guidance on this subject as well.

@EdwinVanVliet
Copy link
Contributor

EdwinVanVliet commented May 3, 2022

@lopukhDA

For what it's worth; I created a PR which allows you to handle the WaitForDebuggerOnStart property correctly using Selenium.
#10603

You can subscribe to the AttachToTarget event on the Target domain. I use the following code to properly attach Selenium to the new DevTools target. When creating the DevToolsSession you should set the WaitForDebugger property to true.

        private void TargetAttachedToTarget(object sender, AttachedToTargetEventArgs e)
        {
            if (e != null && e.TargetInfo != null)
            {
                var commands = new List<DevToolsCommandSettings>();

                //enable domains for page targets to enable generating CDP events.
                if (string.Equals("page", e.TargetInfo.Type, StringComparison.InvariantCultureIgnoreCase))
                {
                    commands.Add(new DevToolsCommandSettings("Page.enable") { SessionId = e.SessionId, CommandParameters = CreateCommand("Page.enable") });
                    commands.Add(new DevToolsCommandSettings("Network.enable") { SessionId = e.SessionId, CommandParameters = CreateCommand("Network.enable") });
                    commands.Add(new DevToolsCommandSettings("Runtime.enable") { SessionId = e.SessionId, CommandParameters = CreateCommand("Runtime.enable") });
                }

                if (e.WaitingForDebugger)
                {
                    commands.Add(new DevToolsCommandSettings("Runtime.runIfWaitingForDebugger") { SessionId = e.SessionId, CommandParameters = CreateCommand("Runtime.runIfWaitingForDebugger") });
                }

                if (commands.Any())
                {
                    //we use Task.Run to prevent blocking other events from Devtools
                    Task.Run(() => _session.SendCommands(commands, millisecondsTimeout: (int)TimeSpan.FromSeconds(5).TotalMilliseconds, throwExceptionIfResponseNotReceived: false));
                }
            }
        }

@github-actions
Copy link

github-actions bot commented Feb 7, 2023

This issue is stale because it has been open 280 days with no activity. Remove stale label or comment or this will be closed in 14 days.

@github-actions github-actions bot added the I-stale Applied to issues that become stale, and eventually closed. label Feb 7, 2023
@github-actions
Copy link

This issue was closed because it has been stalled for 14 days with no activity.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 21, 2023
@nvborisenko
Copy link
Member

Reopened. We need triage it.

@nvborisenko nvborisenko reopened this Oct 24, 2023
@github-actions github-actions bot removed the I-stale Applied to issues that become stale, and eventually closed. label Oct 25, 2023
@titusfortner
Copy link
Member

@nvborisenko can you give a status update on this? Thanks.

@nvborisenko
Copy link
Member

Is in progress.

@fenben5754
Copy link

@nvborisenko do you have any info when this issue will be fixed? In lot of cases when a new tab automatically opened requests can't catch with devtoola
Thank you

@nvborisenko
Copy link
Member

I am afraid nobody is interested in to fix it. Seems fixing this "legacy" issue is risky.

Good news is that BiDi is coming! I already "tried" it, and this use-case is considered at design level of the specification. Any ETA... I would say it is closer than never has been.

@EdwinVanVliet
Copy link
Contributor

@nvborisenko @fenben5754

Just this week I managed to track down the actual issue for which I raised the SendCommands PR a while back, which is the final part which is needed to get this to work. It comes down to the handling of events coming from the browser (DevToolsSession.ProcessIncomingMessages function) and sending commands (DevToolsSession.SendCommand function) are locking each other. However a lot of changes were needed to fix that, but we have it working on our internal repository now.

To summarize what I had to change

  • Within ProcessIncomingMessage there is a BlockingCollection being used for handling events. Changed this to a .NET6+ Channel which allows for async handling. So had to drop .NETStandard2.0 support.
  • The event was delegated to other components using a Task.Run(() => someFunction().GetAwaiter().GetResult()). Changed this to Task.Run(async () => await someFunction().ConfigureAwait(false))
  • Changed all EventHandlers to AsyncEventHandler by changing the codegen,
  • Changes various function which handle events to return tasks instead of void.
  • The SendCommand function used a ManualResetEventSlim for synchronizing sending and receiving commands / messages. Changed to a SemaphoreSlim which allows for async waiting.

This results in the entire flow of raising events from DevTools to responding to those fully async-await. My internal repository is now quite far off from the public repository, so not quite sure what to do with it.

@titusfortner
Copy link
Member

drop .NETStandard2.0 support

This isn't really an option for us right now, anyway.

We're discussing different options for BiDi approach in our chat room if you would like to participate — https://www.selenium.dev/support/#ChatRoom

The current WebDriverBiDi.NET implementation that I started to work with, has decided, based on this conversation, to switch to an Observer pattern. So now we're rethinking what we want to do in Selenium. I think @nvborisenko wants to create his own implementation for Selenium, but I'm really reluctant to have 2 separate implementations maintained by the community.

@nvborisenko
Copy link
Member

nvborisenko commented Jun 28, 2024

@EdwinVanVliet can you elaborate more about

Changes various function which handle events to return tasks instead of void.

What is your use case?

Deprecate my message.

The actual one is: "Do you think we can land your changes into this code base?".

@nvborisenko
Copy link
Member

If you PR the following, please!

The event was delegated to other components using a Task.Run(() => someFunction().GetAwaiter().GetResult()). Changed this to Task.Run(async () => await someFunction().ConfigureAwait(false))

If you PR the following, please!

The SendCommand function used a ManualResetEventSlim for synchronizing sending and receiving commands / messages. Changed to a SemaphoreSlim which allows for async waiting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants