How do I change focus to a new popup tab in Selenium?

后端 未结 7 1239
渐次进展
渐次进展 2020-12-29 06:38

I\'m using Selenium and Firefox.

I have a link on a page (say linkA) that opens a new page in a new tab. The new tab is displayed when linkA is clicked. I then w

相关标签:
7条回答
  • 2020-12-29 06:40

    lericain59's sent me in the right direction, though I had to make a few changes for it work with my version of selenium's IDE (I'm running 1.0.6). Also, for my purposes, I didn't need to verify so much that it opened in a separate window, only that it was opening the correct window.

    Here's the script that worked for me.

    • storeEval | this.browserbot.findElement('link=click here').href | myUrl |
    • open | ${myUrl} ||

    this.page() didn't work. It seems to have been replaced with this.browserbot. Also, I just opened the page directly - it avoids a manual pause and has less steps.

    0 讨论(0)
  • 2020-12-29 06:41

    Did you try adding a windowFocus between selectWindow and click linkB?

    Edit: selectWindow takes a Javascript windowID. Does your linkA specify a windowID for Selenium to access?

    Here is the full first test page (t1.html), in the window.open call the 2nd parameter is 'WindowTest', this is the javascript windowID that selenium looks for.

    <a href="javascript:void(0);" name="t1" 
       onclick="window.open('t2.html', 'WindowTest', 'width=450,height=600');">
     test
    </a>
    

    Here is the second test page (t2.html):

    <a href="t1.html" name="t2">2test2</a>
    

    Running your script ends up with the popup window on t1.html My script

    click              link=test
    pause              5000
    selectWindow       WindowTest
    windowFocus
    click              link=2test2
    
    0 讨论(0)
  • 2020-12-29 06:41

    Simply use this code.

    public void newtab(){
    
        System.setProperty("webdriver.chrome.driver", "E:\\eclipse\\chromeDriver.exe");
    
        WebDriver driver = new ChromeDriver();
    
        driver.get("http://www.w3schools.com/tags/att_a_target.asp");
    
        //I have provided a sample link. Make sure that you have provided the correct link in the above line.
    
        driver.findElement(By.className("tryitbtn")).click();
    
        new Actions(driver).sendKeys(driver.findElement(By.tagName("html")), Keys.CONTROL).sendKeys(driver.findElement(By.tagName("html")), Keys.NUMPAD2).build().perform();
    
    
        // In keyboard we will press 
    
        //ctrl+1 for 1st tab
    
       //ctrl+2 for 2nd tab
    
       //ctrl+3 for 3rd tab.
    
      //Same action is written in the above code.
    
    }
    
    0 讨论(0)
  • 2020-12-29 06:48

    hi try this one ..

    Set<String> winSet = driver.getWindowHandles();
            List<String> winList = new ArrayList<String>(winSet);
            String newTab = winList.get(winList.size() - 1);
            System.out.println("winList: "+winList.size());
            //driver.close(); // close the original tab
            driver.switchTo().window(newTab);
    
    0 讨论(0)
  • 2020-12-29 06:52

    Here are the steps I took for Selenium IDE:

    1. find the link in question
    2. remove the attribute “target” from the link
    3. copy the href destination in a variable (myUrl)
    4. modify the link href->javascript:window.open(myUrl,’myWindow’)
    5. click on link
    6. select window ‘myWindow’

    getEval | this.page().findElement(‘link=click here’).removeAttribute(‘target’)||

    storeEval | this.page().findElement(‘link=click here’).href | myUrl

    getEval | this.page().findElement(‘link=click here’).href=”javascript:window.open(‘${myUrl}’,'myWindow’)” ||

    click | link=click here ||

    pause | 1000 ||

    selectWindow | name=myWindow ||

    0 讨论(0)
  • 2020-12-29 06:57

    It worked for me.

    [info] Executing: |storeEval | this.browserbot.findElement('link=Pastanet').href | Link_PastaNet |
    [info] Executing: |openWindow | ${Link_PastaNet} | MyWindows | 
    
    0 讨论(0)
提交回复
热议问题