在xpath中使用Starts with和end函数查找具有selenium的元素

Ech*_*yak 6 python selenium xpath

我试图从这个输入链接描述中提取所有那些类名称适合正则表达式模式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()
Run Code Online (Sandbox Code Playgroud)

这是我的追溯:

回溯(最近通话最后一个):文件"/home/ubuntu/workspace/vroniplag/vroni.py",线路116,在OP( 'AAF')文件"/home/ubuntu/workspace/vroniplag/vroni.py"线101,在运算plags = getplags(CD)文件"/home/ubuntu/workspace/vroniplag/vroni.py",线92,在getplags断支= driver.find_elements_by_xpath("// [开始-与(@id,' frag-')和ends-with(@ id,' - 0')]")文件"/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py",第305行在find_elements_by_xpath返回self.find_elements(由= By.XPATH,值= 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(消息,屏幕,堆栈跟踪)硒.common.exceptions.InvalidSelectorException:消息:错误消息=>'无法找到带有xpath表达式的元素// [starts-with(@ id,'frag-')和ends-with(@id,' - 0') ]由于以下错误:错误:INVALID_EXPRESSION_ERR:由Request => {"headers"引起的DOM XPath异常51:{"接受":"application/json","Accept-Encoding":"identity","Connection" : "关闭", "内容长度": "139", "内容类型": "应用/ JSON;字符集= UTF-8", "主机": "127.0.0.1:45340","User-Agent" :"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"using \":\"xpath \",\"sessionId \": \"0dbc6ad0-4352-11e6-8cb8-4faebd646180 \",\"value \":\"//*[开头 - (@id,'frag-')和结尾(@id,' - 0' )]\"}", "URL": "/元件", "urlParsed":{ "锚": "", "查询": "", "文件": "元素", "目录": "/" "路径": "/元件", "相对的": "/元件", "端口": "", "宿主": "", "密码": "", "用户": "", "用户信息" : "", "权威": "", "协议": "", "源": "/元件", "queryKey":{}, "块":[ "元素"]} "urlOriginal":"/SES sion/0dbc6ad0-4352-11e6-8cb8-4faebd646180/elements"}屏幕截图:通过屏幕提供

我究竟做错了什么?

And*_*son 9

你可以尝试替换

"//*[starts-with(@id, 'frag-') and ends-with(@id, '-0')]"
Run Code Online (Sandbox Code Playgroud)

"//*[starts-with(@id, 'frag-') and contains(@id, '-0')]"
Run Code Online (Sandbox Code Playgroud)

因为Selenium不支持ends-with选项