1

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);
1
  • What it's printing? Can you please post output that you're getting?
    – user40415
    Jul 12, 2019 at 19:12

1 Answer 1

1

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);
2
  • attribute.getAttribute("href")- this will print the whole URL in the output. I have tried this already. But I wanted the output only few portion of the URL. That is: BGOOE00003.
    – RK007
    Jul 15, 2019 at 6:37
  • If the URL that your trying to get does not keep on changing. Maybe you can extract the part that you want Jul 15, 2019 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.