从动态文本获取网络元素

问题描述 投票:0回答:1

首先,我写我的场景:

  1. 转到http://demo.opencart.com/

  2. search ipod

  3. 单击添加以比较每个发现的项目

这是我的代码(java webdriver pagefactory)

searchresultspage(我的对象页面)

@FindBy(id = "compare-total")
WebElement numberOfProductToCompare;

public void compareAllItems() {
    for (WebElement compareButtons: compareButton) {
        compareButtons.click();
    }
}

public void areAllItemsClickedCompare() {
    String text = numberOfProductToCompare.getText();
    System.out.println(text);
}

我的主要考试班

@Test
public void addToCompare() {
    searchresultspage.compareAllItems();
    searchresultspage.areAllItemsClickedCompare();
}

我单击所有比较按钮,我想从链接Product Compare (4)中获取数字,但是当我使用searchresultspage.areAllItemsClickedCompare();时,则为System.out.println(text);打印我Product Compare (0),即使此方法是添加后进行比较(应该是Product Compare (4))也不知道该怎么办,一些建议?

java selenium selenium-webdriver pageobjects
1个回答
0
投票

认为,出现问题是因为Selenium在实例化类时捕获了compare-total元素,但是当在页面上更新其内容时,它不会影响捕获的值。仅在单击所有复选框后,才尝试捕获compare-total元素:

public void areAllItemsClickedCompare() {
    WebElement numberOfProductToCompare = driver.findElement(By.id("compare-total"));
    String dupa = numberOfProductToCompare.getText();
    System.out.println(dupa);
}

driver是您的WebDriver实例。

© www.soinside.com 2019 - 2024. All rights reserved.