java - 无法使用 Selenium 单击 href 链接

标签 java selenium xpath href

我有一个 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.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();

原因是,chromedriver 很乐意为您打开 Chrome 浏览器,但当您尝试 executeScript("scroll(0,400)").equals( “临床总结”); 但是按照documentation JavascriptExecutor,它是一个接口(interface)和关联的方法executeScriptexecuteAsyncScript 无法调用 click() 方法。

关于java - 无法使用 Selenium 单击 href 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45958066/

相关文章:

java - jsp文件与资源类的连接

python - 使用 Selenium 获取元素的位置

python - 如果按钮只有类型和值,如何使用 Python 在 Web 表单上提交数据

sql-server - 当 URI 未知时,XPath 是否有定义的方法从 namespace 的缩写检索 URI?

java - Android ListView 不会填充 SQLite DB 中的所有条目

java - Jsp算法使用用户输入搜索多个mysql数据库

java - Spring-Hibernate : Insertion error, not-null 约束(一对多关系)

python - 使用 Robot Framework 从 Python 中的方法返回值

selenium - 如何加快 AWS 设备场上的 Selenium 测试速度?

java - 如何使用 Selenium 和 Java 在网页上查找文本并获取其 XPath 或 CSS