1

I need to download the CSV files from this page but i am not able to do so. PFA the screenshot of the page from where the files has to be downloaded :

enter image description here

enter image description here

I have tried the below code but it doesn't work

JavascriptExecutor js = (JavascriptExecutor) wd;          

js.executeScript("arguments[0].click();",wd.findElement(By.xpath("//*[@id='ctrl2016284712']/tbody/tr/td/table/tbody/tr/td/table/tbody/tr[24]/td[7]/a")));
  • 1
    the screenshot is very nice, however a HTML snippet would be much more useful. – user34705 Nov 28 '18 at 9:41
  • Hi Dan, Thanks for the prompt response. I have uploaded the scrennshot of HTML snippet. – Novice Nov 28 '18 at 9:48
  • By.xpath("//*[@id='ctrl2016284712']/, this id must be dynamic in nature thats why your are unable to click, one way to know weather it is dynamic load twice and check id value – sameer joshi Nov 28 '18 at 9:58
  • Yes it is dynamic in nature. So how should i go ahead ? – Novice Nov 28 '18 at 10:10
  • @Novice go with creating dynamic Xpath with contains – sameer joshi Nov 28 '18 at 12:06
0

I guess you could get the 'href' attribute from the anchor tag and just append your base url to it. Then do driver.get(baseURL + href). Also if you face any difficulty creating an xpath then do let me know, I will explain it more in detail.

Updated answer: Try this xpath since you want to download only csv type of file: //table[@class='lcb']/tbody/tr/td/table[@class='ibody']/tbody/tr/td[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),'csv')]/parent::tr/td[7]

  • wd.get("shipper-ml.com/…); I gave this but no success. I am getting "HTTP Status 500 – Internal Server Error" error. – Novice Nov 29 '18 at 6:07
  • Can you help me ? It would be very great. I am stuck on this from a long time. – Novice Nov 29 '18 at 6:08
  • Is it possible to share the url on PM? I could have a look – Leyon Gudinho Nov 29 '18 at 6:09
  • Yes. Can you give me your mail id ? – Novice Nov 29 '18 at 6:12
  • Try this xpath: //table[@class='ibody']/tbody/tr[24]/td[7]/a . Let me know if that works – Leyon Gudinho Nov 29 '18 at 6:14
0

I would not go from the structure in your case but rather from a content. Here is the example of html:

<html>
    <body>
        <table class="table table-condensed">
            <tbody>
                <tr>
                   <th>h1</th>
                   <th>h2</th>
                   <th>h3</th>
                </tr>
                <tr>
                    <td>28112706.CSV</td>
                    <td>12</td>
                    <td>28/11/2018 09:13</td>
                    <td>
                        <a hef="asdfsdf">
                            <img/>
                        </a>
                    </td>
                </tr>
            </tbody>
        </table>
    </body>
</html>

and here is the example of xpath:

//tbody/tr[./td[text()='28112706.CSV']|./td[text()='28/11/2018 09:13']]/td/a

that returns a element that resides in the table row where we have the cell with 28112706.CSV AND the cell with 28112706.CSV

So your challenge is to define the row you want to use and describe the conditionss for the content in xPath expression.

  • Hi Alexey, it's a third party website so I cannot make changes in that. And also the id's are generated dynamically so how to handle that ? – Novice Nov 28 '18 at 10:40
  • @Novice did I mention any changes to the website? I just described how you should build your xpath to define the row you d like to operate with to download file. You should refer to the content, not to the structure. – Alexey R. Nov 28 '18 at 10:41
  • I am not sure how to do it. Can you help me ? – Novice Nov 29 '18 at 5:21
0

If you are looking at downloading all the CSV files, you may want to use findElements() to get all the links and click each one. Looking at the image and HTML snippet, looks like you have a single link in each row pointing to the CSV file.

List<WebElement> allLinks = wd.findElements(By.cssSelector("tr a"));

Should return all the links. Now you can loop through the list and click on each element. Something like:

for(WebElement e: allLinks)
   e.click()
  • Hi Dakshinamurthy, Actually I am not able to point to table because as per my understanding first I should be able to point to table and then only I should go for downloading the files. Isn't it ? wd.findElement(By.xpath("//*[@id='ctrl1857714716']/tbody/tr/td/table/tbody/tr/td/table")); I tried this but no success. – Novice Nov 29 '18 at 5:19
  • You do not need to descend from the table onwards. The above selector should give you all links in each of the row. – Dakshinamurthy Karra Nov 29 '18 at 6:06
  • Couple of points. 1. Prefer CSS selectors to XPath. 2. Have a look at developer.mozilla.org/en-US/docs/Learn/CSS/Introduction_to_CSS/… for a quick introduction to CSS selectors. – Dakshinamurthy Karra Nov 29 '18 at 6:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.