[英]How to write an XPath using AND Operator to add multiple spans in a single XPath?
The page contains a Product name-(3 OF 3) GOLDEN GLOW ( DELUXE ). 该页面包含一个产品名称-(3之3)GLOWEN 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.
产品名称有6个不同的跨度,因此我们要打印产品名称“ GOLDEN GLOW(DELUXE)”,即包括所有跨度,因此我尝试在[]中使用和多次,但没有用。 Below is the XPath:
以下是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: 以下是HTML代码:
<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"
. 应该在
span
之后使用class="totalItems"
选择所有span
节点。 There might be different approaches of extracting required text content depends on Selenium binding. 取决于硒绑定,提取所需文本内容的方法可能有所不同。
This is Python code to get required output: 这是获取所需输出的Python代码:
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! 正如@Michael Kay回答的那样,您需要使用
or
运算符!
You can do this with the findElements
Selenium. 您可以使用
findElements
Selenium进行此findElements
。
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 )"
. 这将返回
WebElements
列表,现在您可以遍历WebElements
并加入文本以创建所需的字符串"GOLDEN GLOW ( DELUXE )"
。
All the credit is to @Michael Kay I just gave you the example... 所有的功劳都归功于@Michael Kay,我只是给您提供示例...
You seem to be confused about the meaning of and
and or
. 您似乎对
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. 谓词中的
and
运算符意味着两个条件都必须成立:它的限制性更强,因此通常选择的数据更少。 The or
operator means either condition must be true: it's more liberal, so more data will be selected. or
运算符表示任何一个条件都必须为真:它更宽松,因此将选择更多数据。
You seem to be thinking of "and" as meaning "union" - select X and (also select) Y. That's never its meaning in boolean logic. 您似乎在想“和”的意思是“联合”-选择X和(同时选择)Y。这在布尔逻辑中从来就没有意义。
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 : 要提取文本Golden Glow(Deluxe),您可以使用以下定位策略 :
Using XPath : 使用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]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.