在xpath

时间:2016-07-06 09:13:30

标签: python selenium xpath

我试图从这个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()

这是我的追溯:

  

追踪(最近一次通话):         文件“/home/ubuntu/workspace/vroniplag/vroni.py”,第116行,in           运算( 'AAF')         在操作文件“/home/ubuntu/workspace/vroniplag/vroni.py”,第101行           plags = getplags(CD)         在getplags中输入“/home/ubuntu/workspace/vroniplag/vroni.py”,第92行           frags = driver.find_elements_by_xpath(“// [starts-with(@ id,'frag-')和ends-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中           提出exception_class(消息,屏幕,堆栈跟踪)       selenium.common.exceptions.InvalidSelectorException:消息:错误消息=> '无法找到带有xpath的元素   表达式// [starts-with(@ id,'frag-')和ends-with(@ id,' - 0')]   由于以下错误:       错误:INVALID_EXPRESSION_ERR:DOM XPath异常51'        由Request =>引起{“报头”:{“接受”:“应用/ JSON”,“接受编码”:“同一性”,“连接”:“关闭”,“内容长度”:“139”,“内容类型”: “应用程序/ JSON;字符集= UTF-8”, “主机”: “127.0.0.1:45340","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method” : “POST”, “邮报”: “{\” 使用\“:   \“xpath \”,\“sessionId \”:\“0dbc6ad0-4352-11e6-8cb8-4faebd646180 \”,   \“value \”:\“// * [开头 - (@id,'frag-')和结尾(@id,   '-0')] \ “}”, “URL”: “/元件”, “urlParsed”:{ “锚”: “”, “查询”: “”, “文件”: “元素”, “目录” : “/”, “路径”: “/元件”, “相对的”: “/元件”, “端口”: “”, “宿主”: “”, “密码”: “”, “用户”: “” “用户信息”: “”, “权威”: “”, “协议”: “”, “源”: “/元件”, “queryKey”:{}, “块”:[ “元素”]}“, urlOriginal “:”/会话/ 0dbc6ad0-4352-11e6-8cb8-4faebd646180 /元件“}       屏幕截图:可通过屏幕获取

我做错了什么?

1 个答案:

答案 0 :(得分:7)

您可以尝试替换

"//*[starts-with(@id, 'frag-') and ends-with(@id, '-0')]"

"//*[starts-with(@id, 'frag-') and contains(@id, '-0')]"

由于Selenium不支持ends-with选项