How to click a href link using Selenium

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

I have a html href link

App Configuration

using Selenium I need

相关标签:
10条回答
  • 2020-12-09 10:40

    Thi is your code :

    Driver.findElement(By.xpath(//a[@href ='/docs/configuration']")).click();
    

    You missed the Quotation mark

    it should be as below

    Driver.findElement(By.xpath("//a[@href='/docs/configuration']")).click();
    

    Hope this helps!

    0 讨论(0)
  • 2020-12-09 10:41
     webDriver.findElement(By.xpath("//a[@href='/docs/configuration']")).click();
    

    The above line works fine. Please remove the space after href.

    Is that element is visible in the page, if the element is not visible please scroll down the page then perform click action.

    0 讨论(0)
  • 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
    0 讨论(0)
  • 2020-12-09 10:53

    Seems like the a tag is hidden. Remember Selenium is not able to interact with hidden element. Javascript is the only option in that case.

    By css = By.cssSelector("a[href='/docs/configuration']");
    WebElement element = driver.findElement(css);
    ((JavascriptExecutor)driver).executeScript("arguments[0].click();" , element);
    
    0 讨论(0)
提交回复
热议问题