简体   繁体   中英

Invalid xPath error in Selenium (with a valid xPath)

I'm testing a Python application using pyccuracy , which has a selenium driver.

My test results are presenting some instability issues with xPaths. Sometimes I get an Invalid xPath error, with a valid xPath. If I run the test again, it may work. I've follow the stacktrace but I couldn't find a solution for that.

Stacktrace:

I do not see "top-search-tag-name" element - FAILED - Error executing action <bound method ElementIsNotVisibleAction.execute of <pyccuracy.actions.core.element_actions.ElementIsNotVisibleAction object at 0x2283b10>> - Traceback (most recent call last):
  File "/workspace/env_test/local/lib/python2.7/site-packages/pyccuracy/fixture_items.py", line 99, in execute
    self.execute_function(context, *self.args, **self.kwargs)
  File "/workspace/env_test/local/lib/python2.7/site-packages/pyccuracy/actions/core/element_actions.py", line 179, in execute
    self.assert_element_is_not_visible(context, element_key, error_message)
  File "/workspace/env_test/local/lib/python2.7/site-packages/pyccuracy/actions/__init__.py", line 147, in assert_element_is_not_visible
    if self.is_element_visible(context, selector):
  File "/workspace/env_test/local/lib/python2.7/site-packages/pyccuracy/actions/__init__.py", line 139, in is_element_visible
    is_visible = context.browser_driver.is_element_visible(selector)
  File "/workspace/env_test/local/lib/python2.7/site-packages/pyccuracy/drivers/core/selenium_driver.py", line 86, in is_element_visible
    is_present = self.selenium.is_element_present(element_selector)
  File "/workspace/env_test/local/lib/python2.7/site-packages/selenium/selenium.py", line 1369, in is_element_present
    return self.get_boolean("isElementPresent", [locator,])
  File "/workspace/env_test/local/lib/python2.7/site-packages/selenium/selenium.py", line 261, in get_boolean
    boolstr = self.get_string(verb, args)
  File "/workspace/env_test/local/lib/python2.7/site-packages/selenium/selenium.py", line 223, in get_string
    result = self.do_command(verb, args)
  File "/workspace/env_test/local/lib/python2.7/site-packages/selenium/selenium.py", line 217, in do_command
    raise Exception, data
Exception: ERROR: Invalid xpath [2]: //form[@id='searchtop']//ul[@class = 'tags block editing']//li/a[@class = 'tag']

HTML Code (cleaned up)

<form id="searchtop">
  <ul class="tags block editing"> 
    <li class="tag"> 
      <a href="#" class="tag">Section Label</a>
      <a href="#" class="remove" title="Remove filter"></a> 
    </li> 
  </ul>
  ...  
</form>

In this test I've clicked the remove filter link and then checked if the element is not present. The xPath is correct. In Chrome, for instance, I get the element selected with $x(...), before removing the tag, and get an empty list after removing it. Which is what the test does. Sometimes the same test runs without errors, in the exact same enviroment.

Ideas?

I see 2 issues here:

  1. Your test seems to continue to run even though there's an error
  2. You're not waiting for the element before executing the next command.

I do not see "top-search-tag-name" element - FAILED - Error executi

This error means.. the element "top-search-tag-name" is not present. usually happens when the application is too slow or not fully loaded. To fix this try to wait for the element before continuing

Exception: ERROR: Invalid xpath [2]: //form[@id='searchtop']//ul[@class = 'tags block editing']//li/a[@class = 'tag']

The xpath seems to be valid. Do you mind changing the xpath to be shorter and give it a shot? //form[@id='searchtop']//[a.='Section Label']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM