Selenium WebDriver new tab and Navigate












1















Based on this post, I managed to open a new tab, but when I try to navigate in the new tab, the navigation occurs in the old tab.



I saw that I should use this:



driver.switchTo().window(windowName);


but what is windowName?










share|improve this question





























    1















    Based on this post, I managed to open a new tab, but when I try to navigate in the new tab, the navigation occurs in the old tab.



    I saw that I should use this:



    driver.switchTo().window(windowName);


    but what is windowName?










    share|improve this question



























      1












      1








      1


      1






      Based on this post, I managed to open a new tab, but when I try to navigate in the new tab, the navigation occurs in the old tab.



      I saw that I should use this:



      driver.switchTo().window(windowName);


      but what is windowName?










      share|improve this question
















      Based on this post, I managed to open a new tab, but when I try to navigate in the new tab, the navigation occurs in the old tab.



      I saw that I should use this:



      driver.switchTo().window(windowName);


      but what is windowName?







      c# selenium






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 20 '17 at 20:47









      budi

      3,49663259




      3,49663259










      asked Aug 27 '14 at 13:58









      Light_UserLight_User

      43210




      43210
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You have to use window handle function here. You asked for a solution in c#. I used java with selenium webdriver. They both would use similar concepts.



          Here is a sample working code in java:



              String parentHandle = driver.getWindowHandle(); // get the current window handle
          System.out.println(parentHandle); //Prints the parent window handle
          String anchorURL = anchor.getAttribute("href"); //Assuming u are clicking on a link which opens a new browser window
          anchor.click(); //Clicking on this window
          for (String winHandle : driver.getWindowHandles()) { //Gets the new window handle
          System.out.println(winHandle);
          driver.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window)
          }
          //Now your driver works on the current new handle
          //Do some work here.....
          //Time to go back to parent window
          driver.close(); // close newly opened window when done with it
          driver.switchTo().window(parentHandle); // switch back to the original window


          Hope this helps!






          share|improve this answer


























          • Unfortunately I don't understand how to convert it. also it seems that you follow from a link and I'm just opening a new tab ( a blank window with no data ) but I think there should be a way to recognize it

            – Light_User
            Aug 27 '14 at 15:07













          • Selenium holds the name of the windows in some strings called handles. In C# we don't use getters and setters, but Properties: driver.WindowHandles is the way to get all the handles, just go for the .Last() and you should get what you want

            – fra9001
            Aug 27 '14 at 15:10











          • Instead of using the for loop you can use this to switch to the windowhandle as fra9001 mentions. driver.SwitchTo().Window(driver.WindowHandles.Last());

            – Fahim
            Aug 27 '14 at 15:29











          • this is how my code looks like ( open new tab + switch ) IWebElement body = driver.FindElement(By.TagName("body")); body.SendKeys(Keys.Control + 't'); driver.SwitchTo().Window(driver.WindowHandles.Last());

            – Light_User
            Aug 27 '14 at 15:34













          • Buy the way , I didn't get the No Window found , but there was no result either

            – Light_User
            Aug 27 '14 at 15:39











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25529160%2fselenium-webdriver-new-tab-and-navigate%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You have to use window handle function here. You asked for a solution in c#. I used java with selenium webdriver. They both would use similar concepts.



          Here is a sample working code in java:



              String parentHandle = driver.getWindowHandle(); // get the current window handle
          System.out.println(parentHandle); //Prints the parent window handle
          String anchorURL = anchor.getAttribute("href"); //Assuming u are clicking on a link which opens a new browser window
          anchor.click(); //Clicking on this window
          for (String winHandle : driver.getWindowHandles()) { //Gets the new window handle
          System.out.println(winHandle);
          driver.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window)
          }
          //Now your driver works on the current new handle
          //Do some work here.....
          //Time to go back to parent window
          driver.close(); // close newly opened window when done with it
          driver.switchTo().window(parentHandle); // switch back to the original window


          Hope this helps!






          share|improve this answer


























          • Unfortunately I don't understand how to convert it. also it seems that you follow from a link and I'm just opening a new tab ( a blank window with no data ) but I think there should be a way to recognize it

            – Light_User
            Aug 27 '14 at 15:07













          • Selenium holds the name of the windows in some strings called handles. In C# we don't use getters and setters, but Properties: driver.WindowHandles is the way to get all the handles, just go for the .Last() and you should get what you want

            – fra9001
            Aug 27 '14 at 15:10











          • Instead of using the for loop you can use this to switch to the windowhandle as fra9001 mentions. driver.SwitchTo().Window(driver.WindowHandles.Last());

            – Fahim
            Aug 27 '14 at 15:29











          • this is how my code looks like ( open new tab + switch ) IWebElement body = driver.FindElement(By.TagName("body")); body.SendKeys(Keys.Control + 't'); driver.SwitchTo().Window(driver.WindowHandles.Last());

            – Light_User
            Aug 27 '14 at 15:34













          • Buy the way , I didn't get the No Window found , but there was no result either

            – Light_User
            Aug 27 '14 at 15:39
















          0














          You have to use window handle function here. You asked for a solution in c#. I used java with selenium webdriver. They both would use similar concepts.



          Here is a sample working code in java:



              String parentHandle = driver.getWindowHandle(); // get the current window handle
          System.out.println(parentHandle); //Prints the parent window handle
          String anchorURL = anchor.getAttribute("href"); //Assuming u are clicking on a link which opens a new browser window
          anchor.click(); //Clicking on this window
          for (String winHandle : driver.getWindowHandles()) { //Gets the new window handle
          System.out.println(winHandle);
          driver.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window)
          }
          //Now your driver works on the current new handle
          //Do some work here.....
          //Time to go back to parent window
          driver.close(); // close newly opened window when done with it
          driver.switchTo().window(parentHandle); // switch back to the original window


          Hope this helps!






          share|improve this answer


























          • Unfortunately I don't understand how to convert it. also it seems that you follow from a link and I'm just opening a new tab ( a blank window with no data ) but I think there should be a way to recognize it

            – Light_User
            Aug 27 '14 at 15:07













          • Selenium holds the name of the windows in some strings called handles. In C# we don't use getters and setters, but Properties: driver.WindowHandles is the way to get all the handles, just go for the .Last() and you should get what you want

            – fra9001
            Aug 27 '14 at 15:10











          • Instead of using the for loop you can use this to switch to the windowhandle as fra9001 mentions. driver.SwitchTo().Window(driver.WindowHandles.Last());

            – Fahim
            Aug 27 '14 at 15:29











          • this is how my code looks like ( open new tab + switch ) IWebElement body = driver.FindElement(By.TagName("body")); body.SendKeys(Keys.Control + 't'); driver.SwitchTo().Window(driver.WindowHandles.Last());

            – Light_User
            Aug 27 '14 at 15:34













          • Buy the way , I didn't get the No Window found , but there was no result either

            – Light_User
            Aug 27 '14 at 15:39














          0












          0








          0







          You have to use window handle function here. You asked for a solution in c#. I used java with selenium webdriver. They both would use similar concepts.



          Here is a sample working code in java:



              String parentHandle = driver.getWindowHandle(); // get the current window handle
          System.out.println(parentHandle); //Prints the parent window handle
          String anchorURL = anchor.getAttribute("href"); //Assuming u are clicking on a link which opens a new browser window
          anchor.click(); //Clicking on this window
          for (String winHandle : driver.getWindowHandles()) { //Gets the new window handle
          System.out.println(winHandle);
          driver.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window)
          }
          //Now your driver works on the current new handle
          //Do some work here.....
          //Time to go back to parent window
          driver.close(); // close newly opened window when done with it
          driver.switchTo().window(parentHandle); // switch back to the original window


          Hope this helps!






          share|improve this answer















          You have to use window handle function here. You asked for a solution in c#. I used java with selenium webdriver. They both would use similar concepts.



          Here is a sample working code in java:



              String parentHandle = driver.getWindowHandle(); // get the current window handle
          System.out.println(parentHandle); //Prints the parent window handle
          String anchorURL = anchor.getAttribute("href"); //Assuming u are clicking on a link which opens a new browser window
          anchor.click(); //Clicking on this window
          for (String winHandle : driver.getWindowHandles()) { //Gets the new window handle
          System.out.println(winHandle);
          driver.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window)
          }
          //Now your driver works on the current new handle
          //Do some work here.....
          //Time to go back to parent window
          driver.close(); // close newly opened window when done with it
          driver.switchTo().window(parentHandle); // switch back to the original window


          Hope this helps!







          share|improve this answer














          share|improve this answer



          share|improve this answer








          answered Aug 27 '14 at 14:43


























          community wiki





          Fahim














          • Unfortunately I don't understand how to convert it. also it seems that you follow from a link and I'm just opening a new tab ( a blank window with no data ) but I think there should be a way to recognize it

            – Light_User
            Aug 27 '14 at 15:07













          • Selenium holds the name of the windows in some strings called handles. In C# we don't use getters and setters, but Properties: driver.WindowHandles is the way to get all the handles, just go for the .Last() and you should get what you want

            – fra9001
            Aug 27 '14 at 15:10











          • Instead of using the for loop you can use this to switch to the windowhandle as fra9001 mentions. driver.SwitchTo().Window(driver.WindowHandles.Last());

            – Fahim
            Aug 27 '14 at 15:29











          • this is how my code looks like ( open new tab + switch ) IWebElement body = driver.FindElement(By.TagName("body")); body.SendKeys(Keys.Control + 't'); driver.SwitchTo().Window(driver.WindowHandles.Last());

            – Light_User
            Aug 27 '14 at 15:34













          • Buy the way , I didn't get the No Window found , but there was no result either

            – Light_User
            Aug 27 '14 at 15:39



















          • Unfortunately I don't understand how to convert it. also it seems that you follow from a link and I'm just opening a new tab ( a blank window with no data ) but I think there should be a way to recognize it

            – Light_User
            Aug 27 '14 at 15:07













          • Selenium holds the name of the windows in some strings called handles. In C# we don't use getters and setters, but Properties: driver.WindowHandles is the way to get all the handles, just go for the .Last() and you should get what you want

            – fra9001
            Aug 27 '14 at 15:10











          • Instead of using the for loop you can use this to switch to the windowhandle as fra9001 mentions. driver.SwitchTo().Window(driver.WindowHandles.Last());

            – Fahim
            Aug 27 '14 at 15:29











          • this is how my code looks like ( open new tab + switch ) IWebElement body = driver.FindElement(By.TagName("body")); body.SendKeys(Keys.Control + 't'); driver.SwitchTo().Window(driver.WindowHandles.Last());

            – Light_User
            Aug 27 '14 at 15:34













          • Buy the way , I didn't get the No Window found , but there was no result either

            – Light_User
            Aug 27 '14 at 15:39

















          Unfortunately I don't understand how to convert it. also it seems that you follow from a link and I'm just opening a new tab ( a blank window with no data ) but I think there should be a way to recognize it

          – Light_User
          Aug 27 '14 at 15:07







          Unfortunately I don't understand how to convert it. also it seems that you follow from a link and I'm just opening a new tab ( a blank window with no data ) but I think there should be a way to recognize it

          – Light_User
          Aug 27 '14 at 15:07















          Selenium holds the name of the windows in some strings called handles. In C# we don't use getters and setters, but Properties: driver.WindowHandles is the way to get all the handles, just go for the .Last() and you should get what you want

          – fra9001
          Aug 27 '14 at 15:10





          Selenium holds the name of the windows in some strings called handles. In C# we don't use getters and setters, but Properties: driver.WindowHandles is the way to get all the handles, just go for the .Last() and you should get what you want

          – fra9001
          Aug 27 '14 at 15:10













          Instead of using the for loop you can use this to switch to the windowhandle as fra9001 mentions. driver.SwitchTo().Window(driver.WindowHandles.Last());

          – Fahim
          Aug 27 '14 at 15:29





          Instead of using the for loop you can use this to switch to the windowhandle as fra9001 mentions. driver.SwitchTo().Window(driver.WindowHandles.Last());

          – Fahim
          Aug 27 '14 at 15:29













          this is how my code looks like ( open new tab + switch ) IWebElement body = driver.FindElement(By.TagName("body")); body.SendKeys(Keys.Control + 't'); driver.SwitchTo().Window(driver.WindowHandles.Last());

          – Light_User
          Aug 27 '14 at 15:34







          this is how my code looks like ( open new tab + switch ) IWebElement body = driver.FindElement(By.TagName("body")); body.SendKeys(Keys.Control + 't'); driver.SwitchTo().Window(driver.WindowHandles.Last());

          – Light_User
          Aug 27 '14 at 15:34















          Buy the way , I didn't get the No Window found , but there was no result either

          – Light_User
          Aug 27 '14 at 15:39





          Buy the way , I didn't get the No Window found , but there was no result either

          – Light_User
          Aug 27 '14 at 15:39




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25529160%2fselenium-webdriver-new-tab-and-navigate%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

          Npm cannot find a required file even through it is in the searched directory