【发布时间】:2018-02-08 00:42:24
【问题描述】:
我有一个 HTML href 链接
<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>
使用 Selenium 我需要点击链接。目前,我正在使用以下代码 -
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();
但它抛出一个错误
“java.lang.NullPointerException”
我也尝试使用 CssSelector 单击按钮,但仍然出现相同的错误。我需要单击临床摘要部分下的下载按钮,但有多个部分具有相同的按钮名称。只有 HREF 是唯一的。
谁能帮帮我?
这段代码:
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();
}
}
【问题讨论】:
-
driver可能为空。你需要初始化它。 -
我认为您首先没有启动驱动程序。可以分享一下你试过的完整代码吗?
-
我已启动驱动程序。所有其他点击,类型等都在工作。我遇到了 HREF 的问题。
-
我已经添加了这段代码。在描述中