简体   繁体   English

有效的XPath表达式

[英]Valid XPath expression

Just two questions: 仅两个问题:

  1. How can I check if the string assigned to a variable corresponds to a valid XPath expression? 如何检查分配给变量的字符串是否对应有效的XPath表达式?
  2. How can I return a customized error message in case the requested resource does not exist? 如果请求的资源不存在,如何返回自定义错误消息?
  1. If the XPath is invalid, you'll get an exception. 如果XPath无效,则会出现异常。
  2. If the requested node does not exist, you'll get an empty result set. 如果请求的节点不存在,您将得到一个空结果集。

For example: 例如:

from lxml import etree
from StringIO import StringIO
tree = etree.parse(StringIO('<foo><bar></bar></foo>'))
try:
  tree.xpath('\BAD XPATH')
  print '1. Valid XPath'
except etree.XPathEvalError, e:
  print '1. Invalid XPath: ', e
if not tree.xpath('/foo/xxx'):
  print '2. Requested node does not exist.'

Runs as follows: 运行方式如下:

1. Invalid XPath:  Invalid expression
2. Requested node does not exist.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不是有效的 XPath 表达式 - Not a valid XPath expression 不是有效的 XPath 表达式 - Is not a valid XPath expression python selenium 说 xpath 表达式无效 - python selenium says xpath expression is not valid 错误:Xpath 不是有效的表达式 Selenium Python - Error: Xpath is not a valid expression Selenium Python 不是有效的 xpath 表达式 driver.find_element_by_xpath - not a valid xpath expression driver.find_element_by_xpath XPath 错误:[contains(text()=&quot;something&quot;)]&#39; 不是有效的 XPath 表达式 - XPath error: [contains(text()="something")]' is not a valid XPath expression selenium - 无法在&#39;Document&#39;上执行&#39;evaluate&#39;:字符串不是有效的XPath表达式 - selenium - Failed to execute 'evaluate' on 'Document': The string is not a valid XPath expression 不是一个有效的 XPath 表达式试图使用 div aria label 包含 - Not a valid XPath expression trying to use div aria label contains 语法错误:无法在“文档”上执行“评估”:字符串 &#39;//img[contains(&#39;1236548597&#39;)]&#39; 不是有效的 XPath 表达式 - SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//img[contains('1236548597')]' is not a valid XPath expression XPath 在 Chrome 中有效,但在 Selenium 中无效 - XPath is valid in Chrome but not in Selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM