Given an HTML string and an XPath expression, returns the absolute XPath of the element. :param html: HTML content as a string :param xpath: XPath expression as a string :return: Absolute XPath string or None if not found
(html, xpath)
| 39 | return html_content |
| 40 | |
| 41 | def get_absolute_xpath(html, xpath): |
| 42 | """ |
| 43 | Given an HTML string and an XPath expression, returns the absolute XPath of the element. |
| 44 | :param html: HTML content as a string |
| 45 | :param xpath: XPath expression as a string |
| 46 | :return: Absolute XPath string or None if not found |
| 47 | """ |
| 48 | try: |
| 49 | tree = etree.HTML(html) |
| 50 | node = tree.xpath(xpath) |
| 51 | if not node: |
| 52 | return None |
| 53 | if isinstance(node, list): |
| 54 | node = node[0] |
| 55 | return build_absolute_xpath(node) |
| 56 | except Exception as e: |
| 57 | print(f"Error: {e}") |
| 58 | return None |
| 59 | |
| 60 | def build_absolute_xpath(node): |
| 61 | """ |
no test coverage detected