Python – Finding elements with selenium using Starts with and ends functions in xpath

pythonseleniumxpath

I am trying to extract all those tags whose class name fits the regex pattern frag-0-0, frag-1-0, etc. from this enter link description here

I am trying the following code

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

This is my traceback:

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

What am I doing wrong?

Best Answer

You can try to replace

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

with

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

as Selenium doesn't support ends-with option

Related Topic