无法使用 Selenium 单击 href link
Unable to click a href link using Selenium
我有一个 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>
使用 Selenium 我需要单击 link。目前,我正在使用以下代码 -
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();
}
}
怎么样,
WebElement element = driver.findElement(By.xpath("//*[@data-target='#recordSharingDownloadModal']")
System.out.println(element)
element.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 Browser
但正如您尝试 executeScript("scroll(0,400)").equals("Clinical Summaries");
但 根据 documentation JavascriptExecutor
这是一个 Interface
和关联的方法 executeScript
和 executeAsyncScript
无法调用 click()
方法。
你试过给 anchor
一个 id
并使用 findElement(By.id(IDTag))
吗?
我使用的替代方法是在元素上使用 sendKeys(ENTER)
。
一个或另一个通常有效。
我有一个 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>
使用 Selenium 我需要单击 link。目前,我正在使用以下代码 -
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();
}
}
怎么样,
WebElement element = driver.findElement(By.xpath("//*[@data-target='#recordSharingDownloadModal']")
System.out.println(element)
element.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 Browser
但正如您尝试 executeScript("scroll(0,400)").equals("Clinical Summaries");
但 根据 documentation JavascriptExecutor
这是一个 Interface
和关联的方法 executeScript
和 executeAsyncScript
无法调用 click()
方法。
你试过给 anchor
一个 id
并使用 findElement(By.id(IDTag))
吗?
我使用的替代方法是在元素上使用 sendKeys(ENTER)
。
一个或另一个通常有效。