My xpath is not working - invalid selector: Unable to locate an element with the xpath expression

1k Views Asked by At

how to select the route number.

<table cellspacing="0" class="route__table" id="route-selection-table">
<tr class="route" route-no="12345">
                <input id="page-uuid" type="hidden" value="241e3808-fff-4564-a8e3-3232238f226a711"><td class="route__number">123459</td>
                <td class="route__name">CORPORATION LIMITED CENTRE</td>
                <td class="route__selected">
                    
                </td>
            </tr>

Iam trying by this xpath ( here route is dynamically changing) so iam passing the value

route = '12345'

 self.driver.find_element_by_xpath("//*[@id='route-selection-table']/tbody/[contains(text(),'" + route + "')]").click()


 selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //*[@id='route-selection-table']/tbody/[contains(text(),'12345')] because of the following error:
E       SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[@id='route-selection-table']/tbody/[contains(text(),'12345')]' is not a valid XPath expression.
E         (Session info: chrome=91.0.4472.77)

..\..\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\errorhandler.py:242: InvalidSelectorException
1

There are 1 best solutions below

0
On BEST ANSWER

Try the following XPath-1.0 expression:

self.driver.find_element_by_xpath("//*[@id='route-selection-table']//*[contains(text(),'" + route + "')]").click()