My question is how can I perform click on element to get data from server response?
Algorithm:
1) Click on div element
<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) Sending request to server
3) Server sends response with modified text.
So I need to get that modified text(TEXT1 in code). I have done it like this :
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();
UPDATE :
As I understand, after getting response from server div element is reloading, not whole page. I need to wait until div element finish reloading. Using debug mode I noticed strange thing. In this line:
phone = webElement.getText();
State of webElement: readyState_ = "loading"
Steps taken :
navigate to https://www.olx.ua/obyavlenie/audi-a4-1998-IDzaH8c.html#c890172227;promoted
Click on Показать (span) on right hand side of the screen.
Code: 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());
}
}
}
Try out this code and let me know the status.
Note : I am using chromeDriver instance to launch chrome browser.
Please let me know if you have concerns related to this.
Update
You can use this code with 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());
}
}
}