使用Selenium和Java处理新打开的标签很困难

问题描述 投票:0回答:1

在我下面的代码中,@Test(priority = 3)处理在新选项卡中打开按钮的问题。但我无法打印标题。我无法理解代码中的逻辑问题。 Selenium不会抛出任何错误消息。但是仍然不会打印新标签的标题

package testNGbasics;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Set;
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.By;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.Test;

    public class Expsrchresult extends CommonVariables {
        @BeforeClass
        public void setup()
            {

            System.setProperty(key,path);
            driver = new ChromeDriver();
            driver.manage().window().maximize();
            driver.manage().deleteAllCookies();
            driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
            }
        @Test (priority=1)
        public void setBaseURL1()
        {
            //Navigating to search results
            driver.get(baseUrl1);
            System.out.println(driver.getCurrentUrl());
        }

        @Test (priority=2)
        public void departresultlist()
        {
          driver.findElement(By.xpath("//span[@class='title-city-text']")).getText();
          driver.findElement(By.xpath("//div[@class='title-date-rtv']")).getText();
            //Printing the search results
            List<WebElement> elements = driver.findElements(By.xpath("//ul[@id='flightModuleList']"));
              for (WebElement webElement : elements) 
              {
                  String srchresult = webElement.getText();
                  System.out.println("The Departure details "+srchresult);
              }

              driver.findElement(By.cssSelector("body.wrap.cf.wrap.cf.l-results.flux-body.fully-responsive.perceived-instant.exp30288-0.exp36256-1.exp36947-1.exp36118-1.exp37254-1.exp32843-0.exp35771-1.exp36140-1.exp29757-0.exp14958-1.exp31247-0.exp36111-1.view-byot:nth-child(2) div.uitk-layout:nth-child(2) div.site-content-wrap.site-content.click-handler-range.cols-row.cf.uitk-grid.uitk-drawer-wrapper.all-grid-fallback-alt.desktop-filter-drawer-disabled:nth-child(16) section.uitk-col.desktop-col-3-4.all-x-gutter-12:nth-child(2) div.bColumn ul.segmented-list.results-list.price-sort li.flight-module.segment.offer-listing:nth-child(1) div.grid-container.standard-padding div.uitk-grid.all-grid-fallback-alt div.uitk-col.all-col-shrink:nth-child(2) div.uitk-grid.all-grid-fallback-alt.all-grid-align-end.align-right.smalltablet-col-1-1 div.uitk-col.standard-col-l-margin.all-col-shrink.display-larger-screens-only:nth-child(2) > button.btn-secondary.btn-action.t-select-btn")).click();

        }
        @Test(priority=3)
        public void returnclick() throws InterruptedException
        {
          driver.findElement(By.xpath("//span[@class='title-city-text']")).getText();
          driver.findElement(By.xpath("//div[@class='title-date-rtv']")).getText();
          //printing return search results
        List<WebElement> elements1 = driver.findElements(By.cssSelector("#flightModuleList"));
          for (WebElement webElement : elements1) 
          {
            String srchresult1 = webElement.getText();
            System.out.println("The Return details "+srchresult1);
        }

          driver.findElement(By.cssSelector("body.wrap.cf.wrap.cf.l-results.flux-body.fully-responsive.perceived-instant.exp30288-0.exp36256-1.exp36947-1.exp36118-1.exp37254-1.exp32843-0.exp35771-1.exp36140-1.exp29757-0.exp14958-1.exp31247-0.exp36111-1.view-byot:nth-child(2) div.uitk-layout:nth-child(2) div.site-content-wrap.site-content.click-handler-range.cols-row.cf.uitk-grid.uitk-drawer-wrapper.all-grid-fallback-alt.desktop-filter-drawer-disabled:nth-child(16) section.uitk-col.desktop-col-3-4.all-x-gutter-12:nth-child(2) div.bColumn ul.segmented-list.results-list.price-sort li.flight-module.segment.offer-listing:nth-child(1) div.grid-container.standard-padding div.uitk-grid.all-grid-fallback-alt div.uitk-col.all-col-shrink:nth-child(2) div.uitk-grid.all-grid-fallback-alt.all-grid-align-end.align-right.smalltablet-col-1-1 div.uitk-col.standard-col-l-margin.all-col-shrink.display-larger-screens-only:nth-child(2) > button.btn-secondary.btn-action.t-select-btn")).sendKeys(Keys.CONTROL,Keys.ENTER);
          Thread.sleep(8000L);
            Set<String> abc = driver.getWindowHandles();
            Iterator<String> it = abc.iterator();

            Thread.sleep(8000L);
            while(it.hasNext())
            {
                driver.switchTo().window(it.next());
                System.out.println(driver.getTitle());     
            }

        }

    }

下面提到测试结果。

PASSED: setBaseURL1
PASSED: departresultlist
PASSED: returnclick

===============================================
    Default test
    Tests run: 3, Failures: 0, Skips: 0
===============================================
java selenium selenium-webdriver webdriverwait window-handles
1个回答
0
投票

[从switchTo()it.next()的窗口后,您需要引起一些等待,以等待Selenium识别新的TAB标题

为了进行快速测试,请添加一些sleeps

Thread.sleep(8000L);

理想情况下,您需要按如下方式收集[[Title之前,将WebDriverWait诱导为titleContains()

new WebDriverWait(driver, 10).until(ExpectedConditions.titleContains("partialPagaTitle")); System.out.println(driver.getTitle());
但是,在通过切换到新的

TAB

之前通过getWindowHandles()收集windowHandles时,您还需要确保两件事:
    带有附加窗口的第二个
  • tab
使用ExpectedConditions.numberOfWindowsToBe(2)正确渲染了>您要切换到的
  • tab
  • 不是父TAB
    所以您的有效代码块将是:

    String first_handle = driver.getWindowHandle(); driver.findElement(By.cssSelector("body.wrap.cf.wrap.cf.l-results.flux-body.fully-responsive.perceived-instant.exp30288-0.exp36256-1.exp36947-1.exp36118-1.exp37254-1.exp32843-0.exp35771-1.exp36140-1.exp29757-0.exp14958-1.exp31247-0.exp36111-1.view-byot:nth-child(2) div.uitk-layout:nth-child(2) div.site-content-wrap.site-content.click-handler-range.cols-row.cf.uitk-grid.uitk-drawer-wrapper.all-grid-fallback-alt.desktop-filter-drawer-disabled:nth-child(16) section.uitk-col.desktop-col-3-4.all-x-gutter-12:nth-child(2) div.bColumn ul.segmented-list.results-list.price-sort li.flight-module.segment.offer-listing:nth-child(1) div.grid-container.standard-padding div.uitk-grid.all-grid-fallback-alt div.uitk-col.all-col-shrink:nth-child(2) div.uitk-grid.all-grid-fallback-alt.all-grid-align-end.align-right.smalltablet-col-1-1 div.uitk-col.standard-col-l-margin.all-col-shrink.display-larger-screens-only:nth-child(2) > button.btn-secondary.btn-action.t-select-btn")).sendKeys(Keys.CONTROL,Keys.ENTER); new WebDriverWait(driver,10).until(ExpectedConditions.numberOfWindowsToBe(2)); Set<String> abc = driver.getWindowHandles(); Iterator<String> itr = abc.iterator(); while(itr.hasNext()) { String nextWindow = itr.next(); if(!firstWindowHandle.equalsIgnoreCase(nextWindow)) { driver.switchTo().window(nextWindow); new WebDriverWait(driver, 10).until(ExpectedConditions.titleContains("partialPagaTitle")); System.out.println(driver.getTitle()); } }


    参考

    您可以在以下位置找到几个相关的讨论:

    最新问题
    © www.soinside.com 2019 - 2024. All rights reserved.