【发布时间】:2016-11-08 06:51:12
【问题描述】:
我正在尝试从 enter link description here 中提取类名符合正则表达式模式 frag-0-0、frag-1-0 等的所有标签
我正在尝试以下代码
driver = webdriver.PhantomJS()
for frg in frgs:
driver.get(URL + frg[1:])
frags=driver.find_elements_by_xpath("//*[starts-with(@id, 'frag-') and ends-with(@id, '-0')]")
for frag in frags:
for tag in frag.find_elements_by_css_selector('[class^=fragmark]'):
lst.append([tag.get_attribute('class'), tag.text])
driver.quit()
这是我的回溯:
Traceback(最近一次调用最后一次): 文件“/home/ubuntu/workspace/vroniplag/vroni.py”,第 116 行,在 操作('Aaf') 文件“/home/ubuntu/workspace/vroniplag/vroni.py”,第 101 行,在 op plags=getplags(cd) 文件“/home/ubuntu/workspace/vroniplag/vroni.py”,第 92 行,在 getplags frags=driver.find_elements_by_xpath("//[starts-with(@id, 'frag-') and end-with(@id, '-0')]") 文件“/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py”, 第 305 行,在 find_elements_by_xpath 中 return self.find_elements(by=By.XPATH, value=xpath) 文件“/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py”, 第 778 行,在 find_elements 中 '价值':价值})['价值'] 文件“/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py”, 第 236 行,执行中 self.error_handler.check_response(响应) 文件“/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py”, 第 192 行,在 check_response 中 引发异常类(消息、屏幕、堆栈跟踪) selenium.common.exceptions.InvalidSelectorException:消息:错误消息 => '无法使用 xpath 找到元素 表达式 //[starts-with(@id, 'frag-') and ends-with(@id, '-0')] 由于以下错误: 错误:INVALID_EXPRESSION_ERR:DOM XPath 异常 51' 由 Request => {"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"139"," Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:45340","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1 ","方法":"POST","发布":"{\"使用\": \"xpath\", \"sessionId\": \"0dbc6ad0-4352-11e6-8cb8-4faebd646180\", \"value\": \"//*[starts-with(@id, 'frag-') 和end-with(@id, '-0')]\"}","url":"/elements","urlParsed":{"anchor":"","query":"","file":"elements","directory" :"/","path":"/elements","relative":"/elements","port":"","host":"","password":"","user":"" ,"userInfo":"","authority":"","protocol":"","source":"/elements","queryKey":{},"chunks":["elements"]}," urlOriginal":"/session/0dbc6ad0-4352-11e6-8cb8-4faebd646180/elements"} 屏幕截图:可通过屏幕获得
我做错了什么?
【问题讨论】:
-
如果您的库不支持 Xpath 2.0,请查看 - stackoverflow.com/questions/22436789/…
-
谢谢。我如何知道安装了哪个版本的 XPath?我正在使用 ubuntu
-
我对python一无所知:(
-
Selenium 仅支持 XPath 1.0 ...