0

Using Java and Selenium, I am trying to get this link: enter image description here

So from what I found, first I do a

Actions action = new Actions(driver);
scrollToElement(href);
action.contextClick(href).perform()

which brings up the menu, as it should. But then I do

action.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).build().perform();
try {
    Thread.sleep(5000);
} catch (InterruptedException e) {

}
action.sendKeys(Keys.RETURN).build().perform();  

However, that seems to do an arrow down OUTSIDE of the context menu. This is a PDF link, so instead of selecting "Save link as", it hits the down arrow OUTSIDE of the context menu, so it closes the context menu, and just left-clicks on the pdf href.

So I am wondering about somehow having it move the arrow down while still in the context box. Or is there an xpath for "Save link as..."? I can't do an inspect on it. I suppose, I could try a

//*[contains(text(), 'Save link as"')]

but not sure that will work or not? Has anyone had this situation?

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Tony
  • 1,127
  • 1
  • 18
  • 29
  • 1
    This might help you https://stackoverflow.com/a/39220537/2466407 – Vardhman Patil Jun 24 '19 at 18:03
  • thanks, but that does not select the right thing from the context menu – Tony Jun 24 '19 at 19:16
  • Maybe you can assign download directory as suggested in link shared by @ANDY_VAR. Then you can just try to download the file by using CTRL+Click instead of context menu like `ActionChains(driver).key_down(Keys.CONTROL).click(href).key_up(Keys.CONTROL).perform()`.(This is python command, which you should change to Java syntax). One more helpful [post](https://stackoverflow.com/questions/53587601/downloading-pdf-from-popup-form-with-selenium-python-chromedriver/53588572#53588572). – Kamal Jun 25 '19 at 02:39
  • I experimented with a lot of things. CTRL-Click did not work, but ALT-ENTER appears to work. However, that may just be on Chrome and not IE? – Tony Jun 25 '19 at 11:29

1 Answers1

0

You're looking into wrong direction, you should not be automating file downloads as you're not testing your application, you're testing the browser and my expectation is that it is not something you should be doing.

Moreover, when you run your test remotely, i.e. in Selenium Grid or locally in parallel mode you will face issues as the browser which is not currently in focus will send key events to the application which is in focus.

The best option is extracting link href attribute value and performing the download using OkHttp library which is under the hood of Selenium Java Client. The relevant code would be something like:

OkHttpClient client = new OkHttpClient().newBuilder().build();

Request request = new Request.Builder().url(href.getAttribute("href")).build();
Response response = client.newCall(request).execute();

File downloadedLogo = new File("myfile.pdf");
BufferedSink sink = Okio.buffer(Okio.sink(downloadedLogo));
sink.writeAll(Objects.requireNonNull(response.body()).source());
sink.close();
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • We have a method, executeFileDownload(), which calls a method we provide to click on a link and intercept the download. It returns a Pair, where Object is the resulting object from clicking the link, and File is the downloaded file. The method determines its location and puts it into File. Problem is, when you just click on the link, it does a view and not a download, so executeFileDownload() will not work. But I see your example above, and I will look into it. Thanks for it (although I guess we are not supposed to say thanks? ;-) – Tony Jun 25 '19 at 11:28
  • There are two OkHttpClients: okhttp3, org.openqa.selenium.remote.internal. Any thoughts on which to use? – Tony Jun 25 '19 at 11:43
  • Here is what I ended up doing. The method was working, however it checked for content-disposition of "attachment" but in this case it was "inline", so when I added a check for "inline" it worked. – Tony Jun 27 '19 at 18:48