python - 使用xpath中的Starts with和ends函数查找selenium元素

标签 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()

这是我的回溯:

Traceback (most recent call last): File "/home/ubuntu/workspace/vroniplag/vroni.py", line 116, in op('Aaf') File "/home/ubuntu/workspace/vroniplag/vroni.py", line 101, in op plags=getplags(cd) File "/home/ubuntu/workspace/vroniplag/vroni.py", line 92, in getplags frags=driver.find_elements_by_xpath("//[starts-with(@id, 'frag-') and ends-with(@id, '-0')]") File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 305, in find_elements_by_xpath return self.find_elements(by=By.XPATH, value=xpath) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 778, in find_elements 'value': value})['value'] File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute self.error_handler.check_response(response) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.InvalidSelectorException: Message: Error Message => 'Unable to locate an element with the xpath expression //[starts-with(@id, 'frag-') and ends-with(@id, '-0')] because of the following error: Error: INVALID_EXPRESSION_ERR: DOM XPath Exception 51' caused by 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","method":"POST","post":"{\"using\": \"xpath\", \"sessionId\": \"0dbc6ad0-4352-11e6-8cb8-4faebd646180\", \"value\": \"//*[starts-with(@id, 'frag-') and ends-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"} Screenshot: available via screen

我做错了什么?

最佳答案

可以尝试替换

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

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

因为 Selenium 不支持 ends-with 选项

关于python - 使用xpath中的Starts with和ends函数查找selenium元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38220287/

相关文章:

python - Python 的性能问题

使用 ctypes 在 C 中动态访问 Python 变量

RS Selenium : Can't see Browser as I run my Code

xpath - 简化 selenium 使用的 xpath 表达式

xml - XSLT 1.0 - 分组 xml 元素

ruby - 搜索 XML 并将节点的子集作为 XML 获取

python - xpath - 跟随兄弟问题

python - ncurses 和白底黑字

java - 当 Selenium 中的 xpath 时,子元素未正确填充

python - 如何在 plot pandas 或 matplotlib python 中停止折线图