How can I write an XPath expression to search for a specific product?

218    Asked by Aalapprabhakaran in QA Testing , Asked on May 23, 2024

 I am currently engaged in a particular task that is related to scraping a website that has a list of product details. Each product is listed in a table row( tr) and the product name is in the first cell (td) with the price in the second cell. How can I write an XPath expression to select the price of the product whose name is “Product A”? 

In the context of selenium, to solve this problem you would need to understand how you can navigate the XML or the HTML document structure by using the Xpath specifically using the “following -sibling” axis.

Xpath is a powerful language to select nodes in an XML document. The “following sibling” axis is used to select all the sibling nodes that would appear after the current node in the document.

Here is the step-by-step approach given to how you can construct the XPath expression for this particular scenario:-

Locating the cell with the product name

You would first need to locate the element which contains the text content related to product A. This can be achieved by using the text () function in Xpath.

Selection of the following siblings

Once you have identified the element which contains product A you can use the “following siblings” axis to select the subsequent element which contains the price.

Here Is the full implementation of the coding given:-

Import requests

From bs4 import BeautifulSoup
From lxml import etree
# Step 1: Fetch the Webpage
url = ‘https://example.com/products’ # Replace with the actual URL
response = requests.get(url)
web_content = response.content
# Step 2: Parse the HTML Content
Soup = BeautifulSoup(web_content, ‘html.parser’)
Html = soup.prettify() # Get a formatted string of the parsed HTML
# Step 3: Convert HTML to lxml Object
Parser = etree.HTMLParser()
Tree = etree.fromstring(html, parser)
# Step 4: Define XPath Expression
Xpath_expression = “//td[text()=’Product A’]/following-sibling::td[1]”
# Step 5: Execute XPath Expression
Price_element = tree.xpath(xpath_expression)
# Step 6: Extract and Print the Price
If price_element:
    Price = price_element[0].text
    Print(f”The price of Product A is: {price}”)
Else:
    Print(“Product A not found or no price available.”)
Here is the example given in python programming language:-
From lxml import html
Import requests
# Fetch the webpage
url = ‘https://example.com/products’ # Replace with the actual URL
response = requests.get(url)
web_content = response.text
# Parse the HTML content
Tree = html.fromstring(web_content)
# Define the XPath expression to select the price of “Product A”
Xpath_expression = “//td[text()=’Product A’]/following-sibling::td[1]”
# Execute the XPath expression
Price_element = tree.xpath(xpath_expression)
# Extract and print the price
If price_element:
    Price = price_element[0].text_content().strip()
    Print(f”The price of Product A is: {price}”)
Else:
    Print(“Product A not found or no price available.”)


Your Answer

Interviews

Parent Categories