How to get the href value from the xpath?



  • Below is my element location, i need to find the id from this link.

    <a href="bnf_view.aspx?QBnfId=BGOOE00003" class="case-hdr" xpath="1">Dean Ambrose</a>
    

    Expecting this output-BGOOE00003

    WebElement Clientlist = driver.findElement(By.xpath("//a[@id='MnClient']"));
    Clientlist.click();
    
    WebElement Clientclick = driver.findElement(By
       .xpath("//*[@id='ctl00_MainContent_ctl00_RadGridList_ctl00__0']/td[2]/a"));
    Clientclick.click();
    Thread.sleep(2000);
    String attribute = driver.findElement(By.xpath("//a[@class='case-hdr']")).getText();
    //WebElement attribute= driver.findElement(Clienthover);
    
    //String attribute = Clienthover.getAttribute("href");
    System.out.println(attribute);
    


  • If you are trying to get the the id of the link that you have mentioned. Please try modified code below.

    WebElement Clientlist = driver.findElement(By.xpath("//a[@id='MnClient']"));
    Clientlist.click();
    
    WebElement Clientclick = driver.findElement(By.xpath("//*[@id='ctl00_MainContent_ctl00_RadGridList_ctl00__0']/td[2]/a"));
    Clientclick.click();
    Thread.sleep(2000);
    String attribute = driver.findElement(By.xpath("//a[@class='case-hdr']"))
    //WebElement attribute= driver.findElement(Clienthover);
    
    attribute.getAttribute("id");
    System.out.println(attribute);
    

    If you want href then you can try below code

    String attribute = driver.findElement(By.xpath("//a[@class='case-hdr']"))
    //WebElement attribute= driver.findElement(Clienthover);
    
    attribute.getAttribute("href");
    System.out.println(attribute);
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2