Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 958
Echchama Nayak Avatar asked Jul 06 '16 09:07

Echchama Nayak


People also ask

How does XPath find elements in Selenium?

Go to the First name tab and right click >> Inspect. On inspecting the web element, it will show an input tag and attributes like class and id. Use the id and these attributes to construct XPath which, in turn, will locate the first name field.

How do I use XPath to find elements?

We can find an element using the xpath locator with Selenium webdriver. To identify the element with xpath, the expression should be //tagname[@attribute='value']. To identify the element with xpath, the expression should be //tagname[@class='value']. There can be two types of xpath – relative and absolute.

How do you write an XPath with an end?

XPath Starts-with. XPath Ends-with. Using “OR” Statement. Using “AND” Statement.

What Startswith () function will do using XPath?

XPath starts-with() is a function used for finding the web element whose attribute value gets changed on refresh or by other dynamic operations on the webpage. In this method, the starting text of the attribute is matched to find the element whose attribute value changes dynamically.


1 Answers

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

like image 92
Andersson Avatar answered Oct 18 '22 02:10

Andersson