java - 为什么我在构造函数上收到 Selenium init 错误?

标签 java selenium pageobjects

抱歉,我不知道我的标题是否正确。但我遇到的问题是,当我创建页面对象 Tile 并在测试页面上实例化它时,它在打开页面后立即出现错误。 代码如下:

public class Tile {
    private WebDriver driver;
    private String title;
    private WebElement titleEl;

    public Tile(WebDriver driver) {
        this.driver = driver;
        this.titleEl = driver.findElement(By.xpath("//div[@id='xpathGoesHere']"));
        this.title = titleEl.getText();
     }

    public WebElement getTitleEl() {
        return titleEl;
    }

    public void setTitleEl(WebElement titleEl) {
        this.titleEl = titleEl;
    }

   public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

并且有一个单独的 Tiles 页面(作为组件)

public class Tiles {
    private WebDriver driver;

    public Tiles(WebDriver driver) {
        this.driver = driver;
    }

另一页:

public class ThePage extends BasePage {

    private final Tile tile;
    private final Tiles tiles;

    public ThePage(WebDriver driver) {
        super(driver);
        this.tile = new Tile(driver);
        this.tiles = new Tiles(driver);
    }

    public Tile tile() {
        return tile;
    }

    public Tilesresults() {
        return tiles;
    }

}

在测试页面上:

public class SomeTest extends BaseTest {
    private static String URL = "https://www.page.com/home/";
private Tile tile;
private ThePage page;
private String link = "link";

@BeforeMethod
public void setup() {
    driver.get(URL);
    driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    page = new ThePage(driver);
}

@Test
public void test() throws Exception {
    // code goes here
    tile = new Tile(driver);
    tiles.results().getTiles();
    tile.getTitle();
}

}

我尝试了很多东西,但我只是不再有想法,我也在线检查了 POM 页面,但找不到为什么它不起作用。请帮忙,我是新手 附:页面如下所示:

enter image description here

最佳答案

在构造函数中添加显式等待,因为元素可能需要一些时间来加载。如果这不起作用,则需要更新 xpath。

WebElement element=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("ur xpath here")));

根据图像Tiles Page Class包含Tile Class,因此这是一种HAS关系,因此我们需要在T​​iles Page Class中添加Tile Class对象引用

public class Tiles {
    private WebDriver driver;
    public Tile tile;
    public Tiles(WebDriver driver) {
        this.driver = driver;
    }

关于java - 为什么我在构造函数上收到 Selenium init 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61414252/

相关文章:

css - 如何编写 CSS 表达式以单击 "Select "我尝试过使用不同的 x 路径它在 IE 任何版本中都不起作用

java - OpenGL ES - 虚线

java - Jersey 2 多部分 pojo 始终为空

java - Selenium 上传文件时样式为 ="display:none;"

selenium - Selenium 网格中的自定义浏览器

selenium - FindBy 属性何时触发 driver.FindElement?

javascript - 在 Nightwatch JS 的页面对象中使用执行命令

python - 返回一个只在某些时候使用的对象

javascript - java.Statement.executeQuery() 创建一个不应该的准备好的语句

java - 将 Java 项目添加到我的 Scala 项目