我可以使用 PageFactory.initElements 创建一个对象,而 WebDriver 还没有直接(还)到存储 WebElements 的页面

Can i use PageFactory.initElements to create an object while WebDriver isn't direct (yet) to the page where the WebElements are stored

我的问题是关于 selenuim 中的页面对象模型。 我将尝试通过一个例子来解释自己 假设我有 2 个站点。 第一个 (www.aaa.com) that has a button that clicking on him take us to the second url (www.bbb.com) 对于这两个页面,我都有一个 class (AAA,BBB),其中包含与每个页面相关的元素。 现在我有以下代码:

@BeforeClass
public void openBrowser() {
    WebDriverManager.chromedriver().setup();
    driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.navigate().to("http://www.aaa.com");
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

    AAA a= PageFactory.initElements(driver, AAA.class);
    BBB b = PageFactory.initElements(driver, BBB.class);
    
}

我的问题是:最后一行代码:BBB b = PageFactory.initElements(driver, BBB.class); 是否会抛出异常,因为对象驱动程序当前设置为与 http://www.aaa.com and therefore when trying to find elements from http://www.bbb.com 一起工作,我们将收到一个异常消息:“NoSuchElement”? 根据我的尝试,它并没有发生,但这对我来说非常令人惊讶,我真的很想了解它是如何正常工作的?我确信只有在移动到第二页后才应该写最后一行(http://www.bbb.com)

PageFactory.initElements 不会抛出 NoSuchElement 异常。原因是,initElements 不会执行 findElement 调用,但会使用元素 proxy.

初始化元素对象