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 - HibernateTemplate Get 方法返回具有空值的对象

java - 如何使这个测试用例失败

c# - Ranorex API 找不到 Ranorex Spy 找到的网络元素

html - Xpath 获取介于 2 个元素之间的元素,我们不能使用 id 或文本来标识第二个标签

c# - 使用 XPath 的 SelectNodes 忽略节点名称中的大小写

java - ViewModel 构造函数应该用@Inject 注释而不是@AssistedInject

java - 生成具有任意数量节点的 XML

java - Postgresql 使用 Java 应用程序发送到后端时发生 I/O 错误

java - 当单选按钮未在 Selenium 中显示时跳过检查

python - Scrapy/BeautifulSoup 模拟 'clicking' 一个按钮以加载网站的一部分