HtmlUnitDriver 单击并等待重新加载元素

HtmlUnitDriver click and wait for reloading element

我的问题是如何执行单击元素以从服务器响应中获取数据? 算法:
1) 点击 div 元素

<div class="contact-button link-phone {'path':'phone', 'id':'z8rpg', 'id_raw': '519183738'} atClickTracking contact-a" data-rel="phone"> 
    <i data-icon="phone"></i> 
    <strong class="xx-large"> TEXT1 </strong> 
</div>

2) 正在向服务器发送请求
3) 服务器发送带有修改文本的响应。 所以我需要获取修改后的文本(代码中的 TEXT1)。我是这样做的:

HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.CHROME);
        driver.get("https://www.olx.ua/obyavlenie/audi-a4-1998-IDzaH8c.html#c890172227;promoted");
        System.out.println("Title: " + driver.getTitle());
        WebElement webElement = driver.findElement
                (By.xpath("//*[@id=\"contact_methods\"]/li[2]/div"));
        webElement.click();
        Thread.sleep(3000);
        webElement = driver.findElement
                (By.xpath("//*[@id=\"contact_methods\"]/li[2]/div"));
        phone = webElement.getText();

更新 :

据我了解,在收到服务器响应后 div 元素正在重新加载,而不是整个页面。 我需要等到 div 元素完成重新加载。 使用调试模式我注意到奇怪的事情。在这一行中:

phone = webElement.getText();

webElement 状态:readyState_ = "loading"

采取的步骤:

  1. 导航至 https://www.olx.ua/obyavlenie/audi-a4-1998-IDzaH8c.html#c890172227;promoted

  2. 单击屏幕右侧的 Показать(跨度)。

  3. 点开后会看到两个数字
  4. 检索这两个数字并将其打印在控制台上。

代码:Java + 硒

public class Lesnov {

    static WebDriver driver;
    static WebDriverWait wait;

    public static void main(String[] args) throws InterruptedException {
         System.setProperty("webdriver.chrome.driver", "D:\Automation\chromedriver.exe");
            driver = new ChromeDriver();
            driver.manage().window().maximize();
            wait = new WebDriverWait(driver, 40);
            driver.get("https://www.olx.ua/obyavlenie/audi-a4-1998-IDzaH8c.html#c890172227;promoted");
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[class^='contact-button']")));
            wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("span[class='spoiler']")));
            driver.findElement(By.cssSelector("span[class='spoiler']")).click();
            wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector("strong[class='xx-large']>span[class='block']")));
            List<WebElement> numbers = driver.findElements(By.cssSelector("strong[class='xx-large']>span[class='block']"));
            for(WebElement num : numbers){
                System.out.println(num.getText());
            }

    }

    }       

试试这个代码,让我知道状态。
注意: 我正在使用 chrome 驱动程序实例来启动 chrome 浏览器。
如果您对此有任何疑虑,请告诉我。

更新

您可以将此代码与 htmlunitDriver 一起使用;

public class Lesnov {

    static WebDriver driver;
    static WebDriverWait wait;

    public static void main(String[] args) throws InterruptedException {
         System.setProperty("webdriver.chrome.driver", "D:\Automation\chromedriver.exe");
         WebDriver driver = new HtmlUnitDriver(BrowserVersion.CHROME ,true);
            driver.manage().window().maximize();
            wait = new WebDriverWait(driver, 40);
            driver.get("https://www.olx.ua/obyavlenie/audi-a4-1998-IDzaH8c.html#c890172227;promoted");
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[class^='contact-button']")));
            wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("span[class='spoiler']")));
            driver.findElement(By.cssSelector("span[class='spoiler']")).click();
            wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector("strong[class='xx-large']>span[class='block']")));
            List<WebElement> numbers = driver.findElements(By.cssSelector("strong[class='xx-large']>span[class='block']"));
            for(WebElement num : numbers){
                System.out.println(num.getText());
            }

    }

    }