Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么在构造函数上出现Selenium init错误?_Java_Selenium_Pageobjects - Fatal编程技术网

Java 为什么在构造函数上出现Selenium init错误?

Java 为什么在构造函数上出现Selenium init错误?,java,selenium,pageobjects,Java,Selenium,Pageobjects,对不起,我不知道我是否正确地构建了标题。但我的问题是,当我创建页面对象Tile并在测试页面上实例化它时,它会在打开页面后给出错误。 代码如下: public class Tile { private WebDriver driver; private String title; private WebElement titleEl; public Tile(WebDriver driver) { this.driver = driver;

对不起,我不知道我是否正确地构建了标题。但我的问题是,当我创建页面对象
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;
}
还有一个单独的分幅页面(作为一个组件)

另一页:

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页面,但找不到为什么它不起作用。请帮帮我,我是新手 另外,页面是这样的:


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

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

根据图像,Tiles页面类包含Tile类,所以这是一种
关系,所以我们需要在Tiles页面类中添加Tile类对象引用

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

根据图像,Tiles页面类包含Tile类,所以这是一种关系,但我无法在Tiles页面类中看到Tile对象引用。对,更正。但事实并非如此。我收到错误(之前也收到过):org.openqa.selenium.NoSuchElementException:找不到元素://div[@id='pathxyz']/。。。在页面上的Tile.(Tile.java:21)在Monsterest.setup(Test.java:24)上的page.java:10)但是定位器是好的,我在开发工具中检查了它。(这行显示>>this.titleEl=driver.findelelement(By.xpath(//div[@id='xpathGoesher']));定位器有可能是错的吗?我很确定它是正确的。我添加了精确的解决方案。@AmitJain可能这是webelement本身的问题。我注意到我在以上述方式初始化它时遇到了问题,即
driver.findElement(By.xpath(//div[@id='xpathGoesher'])
<它总是错误地失败。只有我能用:
getElement(){return driver.findElement(By.xpath(“//xpath”)}
为什么?我不明白
public class Tiles {
    private WebDriver driver;
    public Tile tile;
    public Tiles(WebDriver driver) {
        this.driver = driver;
    }