1

I have this, but it returns the url of the web page. I want the "href" in a text string.

PATH_DATA = //[@id="vvp-product-details-modal--product-title"][@class="a-link-normal"]
WebElement myData = driver.findElement(By.xpath(PATH_DATA));
String url = myData.getAttribute("href")

It returns the url of the web page. I want the "href" in a text string.

Snapshot:

enter image description here

undetected Selenium
  • 151,581
  • 34
  • 225
  • 281
g123
  • 13
  • 3

2 Answers2

0

To print the value of the href attribute you can use either of the following locator strategies:

  • Using cssSelector:

    System.out.println(wd.findElement(By.cssSelector("a.a-link-normal#vvp-product-details-modal--product-title")).getAttribute("href"));
    
  • Using xpath:

    System.out.println(wd.findElement(By.xpath("//a[@class='a-link-normal' and @id='vvp-product-details-modal--product-title']")).getAttribute("href"));
    

Ideally, to extract the the value of the href attribute, you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following locator strategies:

  • Using cssSelector and getText():

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a.a-link-normal#vvp-product-details-modal--product-title"))).getAttribute("href"));
    
  • Using xpath and getAttribute("innerHTML"):

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@class='a-link-normal' and @id='vvp-product-details-modal--product-title']"))).getAttribute("href"));
    
undetected Selenium
  • 151,581
  • 34
  • 225
  • 281
0

Something like this is your best bet:

href = driver.execute_script("return document.querySelector('#vvp-product-details-modal--product-title')?.href")
pguardiario
  • 51,516
  • 17
  • 106
  • 147