1

I have an HTML href link

<a class="btn btn-icon-text btn-secondary" data-toggle="modal" data-target="#recordSharingDownloadModal" href="/person/mL7CD8tR59g2Cy2/health-record/sharing/media/clinical-summary/%7B7c-06-74-be-dc-67-47-5d-be-92-4c-58-5a-fd-1b-8f%7D/download-options/" title="Download Document">Download</a>

using Selenium I need to click the link. Currently, I am using below code -

driver.findElement(By.xpath("//a[contains(@href='/person/mL7CD8tR59g2Cy2/health-record/sharing/media/clinical-summary/%7B7c-06-74-be-dc-67-47-5d-be-92-4c-58-5a-fd-1b-8f%7D/download-options/')]")).click();

But it is throwing an error

"java.lang.NullPointerException"

I have also tried to click the button using CssSelector but still the same error. I need to click a Download button under clinical summary section but there are multiple sections with the same button name. Only the HREF is unique.

Can anyone please help me?

The piece of code:

package LaunchIQHealth;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.*;

public class AutomateIQHealth 
{
    public static void main(String[] args) throws IOException, InterruptedException
    {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\abc\\Desktop\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        WebDriverWait wait = new WebDriverWait(driver, 20);
            driver.get("url");
            ((JavascriptExecutor)driver).executeScript("scroll(0,400)").equals("Clinical Summaries");  
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(text(), 'Download")));      
            driver.findElements(By.xpath("//a[@href='/person/mL7CD8tR59g2Cy2/health-record/sharing/media/clinical-summary/%7B7c-06-74-be-dc-67-47-5d-be-92-4c-58-5a-fd-1b-8f%7D/download-options/']")).get(0).click();
    }
}
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
Tiyasa
  • 13
  • 6
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Guy Aug 30 '17 at 10:34
  • `driver` is probably null. You need to initialize it. – Guy Aug 30 '17 at 10:35
  • I think you haven't initiated the driver first of all. Can you share the complete code that you have tried? – santhosh kumar Aug 30 '17 at 10:36
  • I have initiated the driver. All other clicks, type etc are working. I am facing issue with the HREF. – Tiyasa Aug 30 '17 at 10:39
  • I have added the piece of code. in the description – Tiyasa Aug 30 '17 at 10:48

3 Answers3

0

If I consider your current pasted code as the exact one, you won't be able to do either:

driver.findElement(By.xpath("//a[contains(@href='/person/mL7CD8tR59g2Cy2/health-record/sharing/media/clinical-summary/%7B7c-06-74-be-dc-67-47-5d-be-92-4c-58-5a-fd-1b-8f%7D/download-options/')]")).click();

or

driver.findElements(By.xpath("//a[@href='/person/mL7CD8tR59g2Cy2/health-record/sharing/media/clinical-summary/%7B7c-06-74-be-dc-67-47-5d-be-92-4c-58-5a-fd-1b-8f%7D/download-options/']")).get(0).click();

The reason is, chromedriver happily opens the Chrome Browser for you but as you have tried to executeScript("scroll(0,400)").equals("Clinical Summaries"); but as per the documentation JavascriptExecutor which is an Interface and the associated methods executeScript and executeAsyncScript can't invoke click() method.

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • I have the URL loaded and also the button to be clicked is visible on the screen. I am not allowed to share the URL so I didn't put it up – Tiyasa Aug 30 '17 at 11:10
  • Actually, I need to scroll down in order to make the Download button visible. Can you please let me know some alternate method for the same? – Tiyasa Aug 30 '17 at 11:48
  • @Tiyasa Simply remove the `click()` call, you will be able to scroll down. If my answer have catered to your question Accept the Answer please. – DebanjanB Aug 30 '17 at 11:58
  • I am not sure on what you said.I need to scroll down to the section where the download button is available and for that, I have used ((JavascriptExecutor)driver).executeScript("scroll(0,400)").equals("Clinical Summaries"); But after that I need to click on Download button and I have used the following for that: driver.findElements(By.xpath("//a[@href='/person/mL7CD8tR59g2Cy2/health-record/sharing/media/clinical-summary/%7B7c-06-74-be-dc-67-47-5d-be-92-4c-58-5a-fd-1b-8f%7D/download-options/']")).get(0).click(); Except HREF, there is no other unique value for the button. – Tiyasa Aug 30 '17 at 12:03
  • @Tiyasa As I said if you remove the call to `click()` you would be able to get rid of `NPE`. If your requirement have changed feel free to raise a new question as per your new requirement. – DebanjanB Aug 30 '17 at 12:17
  • As per my question, I am unable to click on a button with the HREF present, if I remove the click(), how will it click then? – Tiyasa Aug 30 '17 at 12:28
  • @Tiyasa Definitely we can't invoke the Java `click()` method inconjunction with the JS call. JS Scroll & Java Click can't be compounded. You have to handle the`click()` later in your code. – DebanjanB Aug 30 '17 at 12:56
0

Have you tried giving the anchor an id and using findElement(By.id(IDTag)) ?

An alternative I use is sendKeys(ENTER) on the element.

One or the other usually works.

iamsankalp89
  • 4,242
  • 2
  • 12
  • 35
-1

how about,

WebElement element = driver.findElement(By.xpath("//*[@data-target='#recordSharingDownloadModal']")
System.out.println(element) 
element.click();
Gaurang Shah
  • 8,589
  • 3
  • 45
  • 90