How to click a href link using Selenium

前端 未结 10 1229
悲哀的现实
悲哀的现实 2020-12-09 09:48

I have a html href link

App Configuration

using Selenium I need

10条回答
  •  粉色の甜心
    2020-12-09 10:49

    To click() on the element with text as App Configuration you can use either of the following Locator Strategies:

    • linkText:

      driver.findElement(By.linkText("App Configuration")).click();
      
    • cssSelector:

      driver.findElement(By.cssSelector("a[href='/docs/configuration']")).click();
      
    • xpath:

      driver.findElement(By.xpath("//a[@href='/docs/configuration' and text()='App Configuration']")).click();
      

    Ideally, to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

    • linkText:

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("App Configuration"))).click();
      
    • cssSelector:

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[href='/docs/configuration']"))).click();
      
    • xpath:

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@href='/docs/configuration' and text()='App Configuration']"))).click();
      

    References

    You can find a couple of relevant detailed discussions in:

    • org.openqa.selenium.ElementNotVisibleException: Element is not currently visible while clicking a checkbox through SeleniumWebDriver and Java
    • How to resolve org.openqa.selenium.ElementNotVisibleException using Selenium with Java in Amazon Sign In

提交回复
热议问题