1

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 )", i.e 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>

Update

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);
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
user3538483
  • 193
  • 2
  • 3
  • 14
  • 1
    Edit your question with HTML sample, your current XPath, current output, desired output – Andersson Jan 03 '19 at 12:37
  • I think you are missing and operator before last cotains. – Amrendra Kumar Jan 03 '19 at 12:41
  • Post XML which will help us to find the problem. – Amrendra Kumar Jan 03 '19 at 12:43
  • I want to prinit the product name i.e Golden Glow ( Deluxe ) ignoring the (3 of 3) so to get this I am trying to use the AND operatier to write the Xptah //span[@class='itemTitleCopy no-mobile' and contains(@class, 'no-mobile') and contains(@class, 'sizeDescriptionTitle no-mobile') and contains(@class, 'no-mobile') ] – user3538483 Jan 03 '19 at 12:58
  • And it didn't work i am able to use and operatior only once i.e //span[@class='itemTitleCopy no-mobile' and contains(@class, 'no-mobile') ] till this I am able to identify the Elment but if I use the all the and operator 3 times I am not able to identify elment. – user3538483 Jan 03 '19 at 12:59
  • @user3538483, how is going? Did you try my answer? – Ratmir Asanov Jan 04 '19 at 09:33

5 Answers5

2
'//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)'
Andersson
  • 51,635
  • 17
  • 77
  • 129
2

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...

Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
1

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.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
0

Use this:

//*[@class=('itemTitleCopy no-mobile','sizeDescriptionTitle no-mobile','no-mobile')]

Hope it will solve.

Amrendra Kumar
  • 1,806
  • 1
  • 7
  • 17
  • org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //*[@class=('itemTitleCopy no-mobile','sizeDescriptionTitle no-mobile','no-mobile')] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[@class=('itemTitleCopy no-mobile','sizeDescriptionTitle no-mobile','no-mobile')]' is not a valid XPath expression. – user3538483 Jan 03 '19 at 13:10
  • This is XPath 2.0 syntax which is not supported by Selenium – Andersson Jan 03 '19 at 13:17
0

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]);
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352