The page contains a Product name-(3 OF 3) GOLDEN GLOW ( DELUXE ). The product name has 6 different spans so we want to print the product name "GOLDEN GLOW ( DELUXE )", ie including the all the spans so I have tried to use the and multiple time inside the [] but it didn't work. Below is the XPath:
//*[@class='itemTitleCopy no-mobile' and contains(@class, 'no-mobile') and contains(@class, 'sizeDescriptionTitle no-mobile') contains(@class, 'no-mobile') ]
Below is the HTML code:
<span class="m-shopping-cart-item-header-number">
(
<span id="itemNo-1" class="itemNo">3</span>
of
<span id="totalItems-1" class="totalItems">3</span>
)
<span class="itemTitleCopy no-mobile" id="itemTitleCopy-1">Golden Glow</span>
<span class="no-mobile">(</span>
<span class="sizeDescriptionTitle no-mobile" id="sizeDescriptionTitle-1">Deluxe</span>
<span class="no-mobile">)</span>
</span>
Code trials:
WebElement checkoutShippingProdName = new WebDriverWait(getDriver(), 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[@class='m-shopping-cart-item-header-number']")));
String shipProdElementHtml = checkoutShippingProdName.getAttribute("innerHTML");
String[] shipProdElementHtmlHtmlSplit = shipProdElementHtml.split("span>");
String currentProd = shipProdElementHtmlHtmlSplit[shipProdElementHtmlHtmlSplit.length -1];
currentProd = StringEscapeUtils.unescapeHtml4(StringUtils.trim(currentProd));
System.out.println("The Product Name is:" + currentProd);
'//span[@class="totalItems"]/following-sibling::span'
should select all span
nodes after span
with class="totalItems"
. There might be different approaches of extracting required text content depends on Selenium binding.
This is Python code to get required output:
text = " ".join([span.text for span in driver.find_elements_by_xpath('//span[@class="totalItems"]/following-sibling::span')])
print(text)
# 'Golden Glow(Deluxe)'
As @Michael Kay has answered what you need is to use to or
operator!
You can do this with the findElements
Selenium.
It should look something like this:
driver.findElements(By.xpath("//*[@class='itemTitleCopy no-mobile' or contains(@class, 'no-mobile') or contains(@class, 'sizeDescriptionTitle no-mobile')]"))
This returns a list of WebElements
now you can iterate through them and join the text to create your desired string of "GOLDEN GLOW ( DELUXE )"
.
All the credit is to @Michael Kay I just gave you the example...
You seem to be confused about the meaning of and
and or
. The and
operator within a predicate means that both conditions must be true: it's more restrictive, so in general less data will be selected. The or
operator means either condition must be true: it's more liberal, so more data will be selected.
You seem to be thinking of "and" as meaning "union" - select X and (also select) Y. That's never its meaning in boolean logic.
Use this:
//*[@class=('itemTitleCopy no-mobile','sizeDescriptionTitle no-mobile','no-mobile')]
Hope it will solve.
To extract the text Golden Glow ( Deluxe ) you can use the following Locator Strategy :
Using XPath :
String myString = driver.findElement(By.xpath("//span[@class='m-shopping-cart-item-header-number']")).getText(); String[] parts = myString.split("?<=)"); System.out.println(parts[1]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.