最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Selenium出错:selenium.common.exceptions.InvalidSelectorException Message invalid selector Compound class names not permitted

selenium crifan 14228浏览 0评论

Selenium的获取元素的代码:

cartNumBtnElemList = driver.find_element_by_class_name(“btn cart-btn-dropdown x-hidden-focus”)

报错:

    cartNumBtnElemList = driver.find_element_by_class_name(“btn cart-btn-dropdown x-hidden-focus”)

  File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py”, line 485, in find_element_by_class_name

    return self.find_element(by=By.CLASS_NAME, value=name)

  File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py”, line 855, in find_element

    ‘value’: value})[‘value’]

  File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py”, line 308, in execute

    self.error_handler.check_response(response)

  File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py”, line 194, in check_response

    raise exception_class(message, screen, stacktrace)

selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Compound class names not permitted

  (Session info: chrome=61.0.3163.100)

  (Driver info: chromedriver=2.33.506106 (8a06c39c4582fbfbab6966dbb1c38a9173bfb1a2),platform=Mac OS X 10.12.6 x86_64)

selenium.common.exceptions.InvalidSelectorException Message invalid selector Compound class names not permitted

python – Selenium Compound class names not permitted – Stack Overflow

去试试css_selector:

cartNumBtnElemList = driver.find_element_by_css_selector(“.btn.cart-btn-dropdown.x-hidden-focus”)

然后看到,其实Chrome开发工具也都已经提示了对应的写法了:

结果出错:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {“method”:”css selector”,”selector”:”.btn.cart-btn-dropdown.x-hidden-focus”}

然后估计才是:

需要写全选择器:

#bb2a6d4-8274-4cc1-85c6-bdb637165983.btn.cart-btn-dropdown.x-hidden-focus

才可以。

但是此处不方便获取这个,看起来是产品的uuid

所以还是换用xpath吧。

cartNumBtnElemList = driver.find_element_by_xpath(‘//button[@class=”btn cart-btn-dropdown x-hidden-focus”]’)

结果竟然还是出错:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {“method”:”xpath”,”selector”:”//button[@class=”btn cart-btn-dropdown x-hidden-focus”]”}

难道是页面没有刷新。去加上:

driver.refresh()

试试,问题依旧。

后来多次调试发现,此处的,以为是产品对应固定的UUID:

bb2a6d4-8274-4cc1-85c6-bdb637165983

实际上不是的,是每次都会变的。

再去根据:

去试试:

cartNumBtnElemList = driver.find_element_by_xpath(‘//div[@id=”ember1354″]/div[@tabindex=”-1″]/button’)

结果,仍是找不到:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {“method”:”xpath”,”selector”:”//div[@id=”ember1354″]/div[@tabindex=”-1″]/button”}

这就有点奇怪了。

好像是需要加上wait等待?

才发现此处的:

id=“ember1354″

是有问题的,因为ember1354每次都会变,比如ember1360

所以才找不到的。

换成:

try:

    # cartNumBtnElemList = WebDriverWait(driver, 10).until(

    #     EC.presence_of_element_located((By.XPATH, ‘//div[@id=”ember1354″]/div[@tabindex=”-1″]/button’)))

    cartNumBtnElemList = WebDriverWait(driver, 10).until(

        EC.presence_of_element_located((By.XPATH, ‘//div[@class=”dropdown ember-view”]/div[@tabindex=”-1″]/button’)))

    logging.info(“cartNumBtnElemList=%s”, cartNumBtnElemList)

    cartNumBtnElem = cartNumBtnElemList[0]

    cartNumBtnElem.click()

except:

    logging.error(“Can not find ms store car number select button”)

    driver.quit()

终于可以找到元素了:

且发现是element,而不是list。

所以去掉wait,不用list,再去试试:

cartNumBtnElem = driver.find_element_by_xpath(‘//div[@class=”dropdown ember-view”]/div[@tabindex=”-1″]/button’)

logging.info(“cartNumBtnElem=%s”, cartNumBtnElem)

cartNumBtnElem.click()

估计也是可以的。

【总结】

此处对于组合的class name:

driver.find_element_by_class_name(“btn cart-btn-dropdown x-hidden-focus”)

不支持,可以改为:

完整的class的链式selector的写法:

driver.find_element_by_css_selector(“.btn.cart-btn-dropdown.x-hidden-focus”)

但是由于此处完整的写法是:

bb2a6d4-8274-4cc1-85c6-bdb637165983.btn.cart-btn-dropdown.x-hidden-focus

但是看似产品UUID的bb2a6d4-8274-4cc1-85c6-bdb637165983实际上每次都会变,导致这么写是找不到元素的,所以只能换成xpath的写法了。

最后是用:

cartNumBtnElem = driver.find_element_by_xpath(‘//div[@class=”dropdown ember-view”]/div[@tabindex=”-1″]/button’)

可以确保始终可以选择对应的元素了。

转载请注明:在路上 » 【已解决】Selenium出错:selenium.common.exceptions.InvalidSelectorException Message invalid selector Compound class names not permitted

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (2)

  1. 你好,我也出现同样的错误,但是我用的是Xpath定位的,直接定位不到这个元素,该怎么办?
    W6年前 (2018-09-13)回复
    • html的代码和要定位的的内容是什么?
      crifan6年前 (2018-09-21)回复
90 queries in 0.200 seconds, using 22.07MB memory