1

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"

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
Eugene Lesnov
  • 188
  • 1
  • 2
  • 13
  • AFAIU : you want to navigate to https://www.olx.ua/obyavlenie/audi-a4-1998-IDzaH8c.html#c890172227;promoted%22 web site and wanna click on div. As soon as you click on that div two phone number are popped up , and you want to retrieve those numbers ? – cruisepandey Apr 14 '18 at 06:44
  • Yes, you are right. I'm working on parser. I'm getting all data but phone number. – Eugene Lesnov Apr 14 '18 at 06:52
  • I'll share the code in some time ! – cruisepandey Apr 14 '18 at 06:52

1 Answers1

0

Steps taken :

  1. navigate to https://www.olx.ua/obyavlenie/audi-a4-1998-IDzaH8c.html#c890172227;promoted

  2. Click on Показать (span) on right hand side of the screen.

  3. Two numbers would be seen as soon as we clicked on it.
  4. Retrieve those two numbers and print it on console.

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());
            }

    }

    }
cruisepandey
  • 28,520
  • 6
  • 20
  • 38