如何使用Selenium在页面的多个子选项卡之间传递驱动程序实例?

时间:2018-09-02 11:56:51

标签: java selenium salesforce

我是Salesforce的新手。我正在尝试在Salesforce页面(闪电体验)中进行自动化。我现在正在工作的页面具有以下设计,

  1. 页面顶部的搜索框
  2. 在同一页面中将搜索结果作为子选项卡

如何将驱动程序实例传递给同一页面的当前显示子选项卡?

目前,当我尝试访问新打开的子选项卡的页面元素时,由于驱动程序仍保持在第一个打开的选项卡并获取其元素的情况下,它引发了异常。

1 个答案:

答案 0 :(得分:0)

您必须从搜索类返回SearchResultTab类对象,该对象将初始化子选项卡的元素。尝试下面的代码。请阅读在Search类中写有注释的代码。

public class Search {

    private WebDriver driver;       
    @FindBy(css=".LM6RPg")
    private WebElement searchTextBox;

    @FindBy(css = ".vh79eN")
    private WebElement searchButton;

    public Search(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    // A method which initializes tab page on submitting search page

    public SearchResults searchInStore(String query) {
        searchTextBox.sendKeys(query + Keys.SPACE);
        searchButton.click();
        return new SearchTab(driver, query);
    }
}

public class SearchTab {

    @FindBy(css="._1ZODb3")
    public WebElement tabElement1;

    @FindBy(how = How.XPATH, using = "//div[@class='_3wU53n']")
    public List<WebElement> tabElement2;



    public SearchResults(WebDriver driver, String query) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
        wait = new WebDriverWait(this.driver,2000);
    }

}

public class SearchTabTest{


   @Test
    public void testProductSearch() {

                Search search = new Search(Browser.driver);
                SearchResults searchResults = search.Search().searchInStore("iphone 5s");
                searchResults.tabElement1.getText();
                searchResults.tabElement2.getText();
    }   

}

public class Browser {

    private static WebDriver driver;

    public static WebDriver driver() {

        System.setProperty("webdriver.chrome.driver", "./resources/browserdrivers/chromedriver.exe");
        DesiredCapabilities caps = DesiredCapabilities.chrome();
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--disable-extensions");
        caps.setCapability(ChromeOptions.CAPABILITY, options);
        driver = new ChromeDriver(caps);
        driver.manage().window().maximize();
        return driver;
    }

    public static void open(String url) {
        driver.get(url);
    }

    public static void close() {
        driver.quit();
    }
}