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"][@]
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:
CodePudding user response:
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"));
CodePudding user response:
Something like this is your best bet:
href = driver.execute_script("return document.querySelector('#vvp-product-details-modal--product-title')?.href")