seleniumbase / SeleniumBase

📊 Python's all-in-one framework for web crawling, scraping, testing, and reporting. Supports pytest. UC Mode provides stealth. Includes many tools.
https://seleniumbase.io
MIT License
4.67k stars 929 forks source link

Xpath to CSS converter error ? #1718

Closed molinacarlos closed 1 year ago

molinacarlos commented 1 year ago

Greetings.

Trying to use some xpath selectors, but get this error from seleneiumbase, trying to use the following selector

//*[text()='Identificación']//following::input

In a test case.

xpath = "//*[text()='Identificación']/descORself/following::input", original = "//*[text()='Identificación']//following::input"

    def _get_raw_css_from_xpath(xpath, original):
        css = ""
        attr = ""
        position = 0

        while position < len(xpath):
            node = prog.match(xpath[position:])

[9] > ...\lib\site-packages\seleniumbase\fixtures\xpath_to_css.py(94)
  ..
  83     def _get_raw_css_from_xpath(xpath, original):
  84         css = ""
  85         attr = ""
  86         position = 0
  87
  88         while position < len(xpath):
  89             node = prog.match(xpath[position:])
  90             if node is None:
  91                 raise XpathException(
  92                     "\nInvalid or unsupported XPath:\n%s\n"
  93                     "(Unable to convert XPath Selector to CSS Selector)"
  94  ->                 "" % original
  95                 )
  96             match = node.groupdict()
  97
  98             if position != 0:
  99                 nav = " " if match["nav"] == "//" else " > "
 100             else:
 101                 nav = ""
 102
 103             tag = "" if match["tag"] == "*" else match["tag"] or ""
 104     ...

Note at this line xpath = "//*[text()='Identificación']/descORself/following::input", original = "//*[text()='Identificación']//following::input"

See the "descORself" string

IMPORTANT: It works on selbase 1.37.14, but isn't work on selbase 4.9.5

Thanks in advance for any advice :-)

mdmintz commented 1 year ago

In xpath, :: does not have an equivalent CSS Selector operator. (Note the following Stack Overflow post, https://stackoverflow.com/questions/20072319/css-equivalents-for-xpath-axes, which has been open for 10 years without an answer.)

If using CSS Selectors is necessary for something that you're doing in SeleniumBase (such as for a JavaScript call), then try a different xpath selector, or use a CSS Selector directly. The SeleniumBase Recorder can help you generate a script with the correct selectors. (See https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/recorder_mode.md)

NOTE: There are CSS Selectors that use :: (https://www.w3schools.com/cssref/css_selectors.php), but it means something different from the xpath ::. SeleniumBase also has the ::shadow selector (https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/shadow_dom.md).