无法在硒中的Chrome浏览器的新标签中设置焦点?

时间:2017-02-13 05:34:25

标签: selenium selenium-webdriver

我正在尝试在新标签上阅读对象,但是驱动程序焦点并没有改变。这是一个场景: 1.打开Gmail。 2.单击Gmail站点下方的“术语”链接。新标签将打开。 3.从术语链接的新选项卡中读取任何对象。

无法执行第3步。这是代码

WebDriver driver= new ChromeDriver();
    driver.get("http://gmail.com");
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    Thread.sleep(2000);

    String Mainwindow = driver.getWindowHandle();

    boolean Term = driver.findElement(By.xpath("//*[@id='footer-list']/li[3]/a")).isDisplayed();
    if(Term){
        driver.findElement(By.xpath("//*[@id='footer-list']/li[3]/a")).click();
    }
    else{
        driver.findElement(By.xpath("html/body/footer/div/div[5]/div[1]/a[4]")).click(); 
        // this is written for IE and Mozila
    }

    Set<String> set = driver.getWindowHandles();
    System.out.println(set.size());

    Iterator<String> it = set.iterator();   
    String main =it.next();
    String maintab =it.next();
    System.out.println(maintab);



    driver.switchTo().window(maintab);
    Thread.sleep(10000);
    driver.findElement(By.xpath("//*[@id='maia-nav-y']/ul/li[1]")).sendKeys(Keys.PAGE_DOWN);
    Thread.sleep(3000);
    driver.findElement(By.xpath(".//*[@id='maia-nav-y']/ul/li[1]")).sendKeys(Keys.PAGE_UP);
    Thread.sleep(3000);
    driver.findElement(By.xpath(".//*[@id='maia-nav-y']/ul/li[1]")).sendKeys(Keys.BACK_SPACE);

    driver.switchTo().window(main);


        }}

3 个答案:

答案 0 :(得分:1)

使用数组列表而不是设置。由于Set使用称为散列的机制来存储元素。所以String&#39; main&#39;或者&#39; mainTab&#39;可能没有正确的窗口手柄来切换。

答案 1 :(得分:0)

Webdriver已成功切换到新标签页。但是,您尝试在不是输入元素的控件上调用 sendKeys API。

如果您只想在新标签页中向上和向下滚动页面,可以使用 JavascriptExecutor 执行此操作,如下面的代码所示:

WebDriver driver= new ChromeDriver();
driver.get("http://gmail.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(2000);

String Mainwindow = driver.getWindowHandle();



boolean Term = driver.findElement(By.xpath("//*[@id='footer-list']/li[3]/a")).isDisplayed();
    if(Term){
           driver.findElement(By.xpath("//*[@id='footer-list']/li[3]/a")).click();
    }
    else{
         driver.findElement(By.xpath("html/body/footer/div/div[5]/div[1]/a[4]")).click(); 
                // this is written for IE and Mozila
}

Set<String> set = driver.getWindowHandles();
System.out.println(set.size());

Iterator<String> it = set.iterator();   
String main =it.next();
String maintab =it.next();
System.out.println(maintab);



driver.switchTo().window(maintab);
Thread.sleep(10000);

//Using JavaScriptExecutor
//Scroll page down
JavascriptExecutor jscriptExec = (JavascriptExecutor)driver;
jscriptExec.executeScript("window.scrollBy(0,250)", "");

//Scroll page up
jscriptExec.executeScript("window.scrollBy(0,-250)", "");



//driver.findElement(By.xpath("//*[@id='maia-nav-y']/ul/li[1]")).sendKeys(Keys.PAGE_DOWN);
//Thread.sleep(3000);
//driver.findElement(By.xpath(".//*[@id='maia-nav-y']/ul/li[1]")).sendKeys(Keys.PAGE_UP);
//Thread.sleep(3000);
//driver.findElement(By.xpath(".//*[@id='maia-nav-y']/ul/li[1]")).sendKeys(Keys.BACK_SPACE);

driver.switchTo().window(main);

driver.quit();

答案 2 :(得分:0)

可能你需要试试这个:

Cat Processor::process(const Cat &, const Cat &) const