1

I have been trying to check the balances of Starbucks gift cards in my application, https://www.starbucks.com/card

I was trying to catch the response made with ajax request, once check balance button is clicked automated, How can I get the ajax response in Selenium?

using (var driver = new ChromeDriver())
        {
            driver.Navigate().GoToUrl(@"https://www.starbucks.com/card");
            driver.FindElement(By.Id("Card_Number")).SendKeys("7848549479363805");
            driver.FindElement(By.Id("Card_Pin")).SendKeys("91435723");
            driver.FindElement(By.XPath("//button[.='Check Balance']")).Click();
        }

How can I read this response as I am unable to with above code?

Below is the Response after Ajax request:

<div id="cardBalanceWrapper">

<div id="card-balance-display" class=" region reset-left">
<img src="https://www.starbucks.com/images/cardimages/card_altdistcoreFY13_165.png" alt="Standard"></div>
    <div id="fetch_balance" class="region size1of3 reset">
    <strong>Card balance</strong>
<h2><span class="fetch_balance_value">$0.00</span></h2> <p>as of <span class="date">6/19/2017 6:56 PM</span></p></div>
<div class="fields region size1of1 reset-left">
<ul class="balance_links">

        <li><a href="/account/signin">Register This Card</a></li>

    <li><a href="/account/card/Reload">Reload This Card</a></li>
    <li><a href="/card/manage/history">View Card Transactions</a></li>
    <li><hr></li>
    <li><a href="/card?showdefault=False">Enter Another Card</a></li>
</ul>

Khawaja Asim
  • 1,327
  • 18
  • 38
  • You cannot use Selenium to get the AJAX response. However, you can get the updated balance value in the HTML after it clicks on the update. You just need to add `Wait` before finding the `fetch_balance_value` element. – Buaban Jun 20 '17 at 02:39
  • @Buaban does PageSource of driver object gets updated, after dynamic html binding to view? and how much wait ? wait will not be an effective solution, right ? – Khawaja Asim Jun 20 '17 at 04:21
  • 1. Yes, the page source will be updated after the binding. 2. Wait until the balance element appears. You can google some examples. – Buaban Jun 20 '17 at 04:41
  • @Buaban ah thankyou, let me try I will get back to you :) – Khawaja Asim Jun 20 '17 at 04:54
  • @Buaban or is there any other way you may know we can integrate balance checking feature to our application rather than scrapping? – Khawaja Asim Jun 20 '17 at 05:01
  • I don't have starbuck member so I can't suggest, sorry. – Buaban Jun 20 '17 at 05:15
  • @Buaban why i cannot use Selenium to get the AJAX response? – Alex78191 Jul 19 '17 at 10:55
  • @Alex78191 Selenium doesn't support it. You can use Selenium to execute JS to fetch or AJAX and access these response. But you cannot access AJAX which was made by the web application. However, if you are the owner of the web application, you may save the AJAX response to variables and then expose these variables to Selenium. Then you can use Selenium to access them. – Buaban Jul 19 '17 at 11:03
  • @Buaban First you said "You cannot use Selenium to get the AJAX response", but now "You can use Selenium to access these response". – Alex78191 Jul 19 '17 at 11:12
  • @Alex78191 as I mentioned, you can access AJAX response which was made by Selenium. – Buaban Jul 19 '17 at 11:14
  • @Buaban I did not understand at first what you are saying about a particular request. – Alex78191 Jul 19 '17 at 11:18
  • It may be months after it but here is a solution for the ones seeking it: https://stackoverflow.com/questions/6884616/intercept-all-ajax-calls – Boris D. Teoharov Nov 26 '17 at 04:52

1 Answers1

0

To fetch ajax requests you can use rest-assured
It is more fast and reliable way to get data. Do not use Selenium for such purposes.

nick318
  • 575
  • 4
  • 18