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();
    }
}
5
  • 2
    Possible duplicate of What is a NullPointerException, and how do I fix it? Commented Aug 30, 2017 at 10:34
  • driver is probably null. You need to initialize it. Commented Aug 30, 2017 at 10:35
  • I think you haven't initiated the driver first of all. Can you share the complete code that you have tried? Commented Aug 30, 2017 at 10:36
  • I have initiated the driver. All other clicks, type etc are working. I am facing issue with the HREF. Commented Aug 30, 2017 at 10:39
  • I have added the piece of code. in the description Commented Aug 30, 2017 at 10:48

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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
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 Simply remove the click() call, you will be able to scroll down. If my answer have catered to your question Accept the Answer please.
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 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.
|
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.

Comments

-1

how about,

WebElement element = driver.findElement(By.xpath("//*[@data-target='#recordSharingDownloadModal']")
System.out.println(element) 
element.click();

3 Comments

and what does it prints in step 2??
Sorry I didn't get you. Which print?
System.out.println(element)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.