【问题标题】:Unable to click a href link using Selenium无法使用 Selenium 单击 href 链接
【发布时间】: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 的问题。
  • 我已经添加了这段代码。在描述中

标签: java selenium xpath href


【解决方案1】:

怎么样,

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

【讨论】:

  • 它在第 2 步中打印了什么??
  • 对不起,我没有得到你。哪个印刷品?
  • System.out.println(element)
【解决方案2】:

如果我认为您当前粘贴的代码与您当前粘贴的代码完全相同,那么您将无法做到:

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();

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 Browser,但正如您尝试 executeScript("scroll(0,400)").equals("Clinical Summaries"); 一样,documentation JavascriptExecutor 这是一个 Interface 并且关联的方法 executeScriptexecuteAsyncScript 不能调用 click() 方法。 p>

【讨论】:

  • 我已加载 URL,并且要单击的按钮在屏幕上可见。我不允许分享网址,所以我没有贴出来
  • 实际上,我需要向下滚动才能使“下载”按钮可见。你能告诉我一些替代方法吗?
  • @Tiyasa 只需删除click() 调用,您就可以向下滚动。如果我的回答符合您的问题,请接受。
  • 我不确定你说的是什么。我需要向下滚动到下载按钮可用的部分,为此,我使用了 ((JavascriptExecutor)driver).executeScript("scroll( 0,400)").equals("临床总结");但在那之后我需要点击下载按钮,我为此使用了以下内容: 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();除了 HREF,按钮没有其他唯一值。
  • @Tiyasa 正如我所说,如果您删除对click() 的调用,您将能够摆脱NPE。如果您的要求发生了变化,请随时根据您的新要求提出新问题。
【解决方案3】:

您是否尝试过给anchor 一个id 并使用findElement(By.id(IDTag))

我使用的另一种方法是在元素上使用sendKeys(ENTER)

一个或另一个通常有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 2018-02-27
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多