如何在另一个选项卡上获取新URL然后再进行处理?

时间:2016-02-26 12:19:19

标签: java selenium-webdriver

测试场景

  1. 使用网址" www.xyz.com"
  2. 注册您的帐户
  3. 验证注册成功消息。
  4. 现在想要打开另一个标签并获取另一个新网址" http://www.gmail.com/"通过单击激活链接激活注册。
  5. 目前,我可以打开另一个标签,但这些标签无法获取Gmail网址。此外,我正在使用关键字驱动框架。

    那么,任何人都可以帮我解决它吗?

    方法中的条件

        if(operation.equalsIgnoreCase("urlOnNewTab")){
                        temp.sendKeys(Keys.CONTROL+"t");
                        ArrayList<String> tab = new ArrayList<String>(driver.getWindowHandles());
                        driver.switchTo().window(tab.get(1));
    
                        /*String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN); 
                        temp.sendKeys(selectLinkOpeninNewTab);*/
    
                        driver.get(value);
    
                        if (operation.equalsIgnoreCase("SendKey")) {
                            temp.sendKeys(value);
                        }
    
                        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
                        if (operation.equalsIgnoreCase("Click")) {
                            temp.click();
                        }
                        if (operation.equalsIgnoreCase("Verify")) {
                            System.out.println("Verify--->" + temp);
                            temp.isDisplayed();
                        }
                    }
    
    
    public WebElement getElement(String locator, String objectName)
                throws Exception {
            WebElement temp = null;
    
            System.out.println("Locator-->" + locator);
            if (locator.equalsIgnoreCase("id")) {
                temp = driver.findElement(By.id(objectName));
    
            } else if (locator.equalsIgnoreCase("xpath")) {
                temp = driver.findElement(By.xpath(objectName));
                System.out.println("xpath temp ----->" + temp);
            } else if (locator.equalsIgnoreCase("name")) {
                temp = driver.findElement(By.name(objectName));
            }else if (locator.equalsIgnoreCase("linkText")) {
                temp = driver.findElement(By.linkText(objectName));
            }else if (locator.equalsIgnoreCase("cssSelector")) {
                temp = driver.findElement(By.cssSelector(objectName));
            }
    
            return temp;
    

1 个答案:

答案 0 :(得分:0)

我没有得到任何人的反馈,但我尝试了下面的代码,根据我的问题它对我有用。

<强>代码:

 if(operation.equalsIgnoreCase("urlOnNewTab")){
                    temp.sendKeys(Keys.CONTROL+"t");
                    driver.get(value);
                    if (operation.equalsIgnoreCase("SendKey")) {
                        temp.sendKeys(value);
                    }

                    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
                    if (operation.equalsIgnoreCase("Click")) {
                        temp.click();
                    }
                    if (operation.equalsIgnoreCase("Verify")) {
                        System.out.println("Verify--->" + temp);
                        temp.isDisplayed();
                    }
                }

    public WebElement getElement(String locator, String objectName)
                throws Exception {
            WebElement temp = null;

            System.out.println("Locator-->" + locator);
            if (locator.equalsIgnoreCase("id")) {
                temp = driver.findElement(By.id(objectName));

            } else if (locator.equalsIgnoreCase("xpath")) {
                temp = driver.findElement(By.xpath(objectName));
                System.out.println("xpath temp ----->" + temp);
            } else if (locator.equalsIgnoreCase("name")) {
                temp = driver.findElement(By.name(objectName));
            }else if (locator.equalsIgnoreCase("cssSelector")) {
                temp = driver.findElement(By.cssSelector(objectName));
            }

            return temp;