Need to get href from HTML through section id
As far as i understood your question. you want to extract all the links from the mentioned section.
Use below code and let me know if further queries.
WebElement section = driver.findElement(By.id("product-page"));List<WebElement> links = section.findElement(By.cssSelector(".breadcrumb a"));System.out.println("total links : " + links.size());for(int i=0 ; i<links.size(); i++){ System.out.println("Linktext : " + links.get(i).getText()); System.out.println("Link href : " + links.get(i).getAttribute("href"));}
To retrieve the href attribute which is /e-cigs-vaping-devices/logic-pro-vaporizer
you can use the following solution:
xpath
:System.out.println(driver.findElement(By.xpath("//section[@id='product-page']//ul[@class='breadcrumb']//a[contains(.,'Logic pro vaporizer')]")).getAttribute("href"));
Update
You can create a custom function and call it whereever needed as follows:
Function :
public void print_href(String productName){ System.out.println(driver.findElement(By.xpath("//section[@id='product-page']//ul[@class='breadcrumb']//a[contains(.='" + productName + "')]")).getAttribute("href"));}
Calling the function :
print_href("Logic pro vaporizer")//orprint_href("E cigs vaping devices")