我的问题是如何执行单击元素以从服务器响应中获取数据?算法:
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_ = "加载">
采取的步骤:
-
导航到 https://www.olx.ua/obyavlenie/audi-a4-1998-IDzaH8c.html#c890172227;promoted
-
点击 Показать (跨度( 在屏幕的右侧。
- 我们点击它后立即会看到两个数字。
- 检索这两个数字并在控制台上打印。
代码:Java+Selenium
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());
}
}
}
试试这个代码,让我知道状态。
注意:我正在使用chromeDriver实例来启动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());
}
}
}