gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:12:38 25 4
gpt4 key购买 nike

抱歉,我不知道我的标题是否正确。但我遇到的问题是,当我创建页面对象 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com