最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】selenium中点击列表选项出错selenium.common.exceptions.ElementNotVisibleException Message element not visible

selenium crifan 377浏览 0评论

代码:

cartNumOptionElemList = driver.find_elements_by_xpath(‘//ul[@class=”dropdown-menu”]/li[@role=”option”]’)

cartNumOptionCount = len(cartNumOptionElemList)

logging.debug(“cartNumOptionElemList=%s,cartNumOptionCount=%s”, cartNumOptionElemList, cartNumOptionCount)

# toSelectNum = toProcessInfoDict[“orderNumber”]

toSelectNum = cartNumOptionCount

if cartNumOptionCount > toProcessInfoDict[“orderNumber”]:

    logging.debug(“Current Cart select max number %s > remain buy number %s, so only need select %s”,

                  cartNumOptionCount, toProcessInfoDict[“orderNumber”], toProcessInfoDict[“orderNumber”])

    toSelectNum = toProcessInfoDict[“orderNumber”]

logging.info(“Found cart number drop down list”)

toSelectIdx = toSelectNum – 1

carNumSelectElem = cartNumOptionElemList[toSelectIdx]

logging.debug(“carNumSelectElem=%s”, carNumSelectElem)

carNumSelectElem.click()

log:

    carNumSelectElem.click()

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

    self._execute(Command.CLICK_ELEMENT)

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

    return self._parent.execute(command, params)

  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.ElementNotVisibleException: Message: element not visible

  (Session info: chrome=63.0.3239.84)

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

想要:鼠标滚动到可选择列表的底部

(然后再去点击)

很明显是还不可见就点击所以报错。

selenium.common.exceptions.ElementNotVisibleException Message element not visible

python selenium自动化(三)Chrome Webdriver的兼容 – harolei – 博客园

selenium.common.exceptions.ElementNotVisibleException: Message: element not visible处理方法:selenium针对下拉菜单事件的处理 – 波比12 – 博客园

selenium.common.exceptions.ElementNotVisibleException: Message: element not visible using Chrome browser 53 and Chrome driver 2.24 and Selenium 2.53.1 · Issue #2867 · SeleniumHQ/selenium

selenium Message element not visible how scroll

selenium  how scroll to option list

selenium – How to perform a scroll in a drop down list to hidden element? – Stack Overflow

selenium scroll drop down list

Handling Dropdowns | Selenium Cooked With Python and Spiced With Java

selenium scroll

How to scroll web page UP or Down using Selenium WebDriver? | Cloud Testing Tools, Ideas and Tips

Multiple Ways to Scroll a Page Using Selenium WebDriver – The Official 360logica Blog

Page scroll up or down in Selenium WebDriver (Selenium 2) using java – Stack Overflow

How to Scroll into view in Selenium Webdriver

selenium python

8. Appendix: Frequently Asked Questions — Selenium Python Bindings 2 documentation

7. WebDriver API — Selenium-Python中文文档 2 documentation

Window scrollTo() Method

后来继续测试,结果又没法选择滚动去选择最后的30了,因为此时只能选择5个了:

不过暂时对于代码:

toSelectIdx = toSelectNum – 1

carNumSelectElem = cartNumOptionElemList[toSelectIdx]

logging.debug(“carNumSelectElem=%s”, carNumSelectElem)

# if not carNumSelectElem.is_displayed():

logging.info(“to select cart number element MAYBE invisible, now to scroll to visible”)

driver.execute_script(“arguments[0].scrollIntoView(true);”, carNumSelectElem)

去选择5个,是可以工作的:

【后记】

但是发现,更多的时候,对于5个时候,一次性都可以显示的时候:

结果代码都是出错:

<code>    carNumSelectElem.click()
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 501, in _execute
    return self._parent.execute(command, params)
  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.ElementNotVisibleException: Message: element not visible
  (Session info: chrome=63.0.3239.84)
  (Driver info: chromedriver=2.33.506106 (8a06c39c4582fbfbab6966dbb1c38a9173bfb1a2),platform=Mac OS X 10.13.2 x86_64)
</code>

所以,看来这个办法是有问题的。

或者是需要优化的。

所以暂时还是注释掉代码。

等后续有30个可以选择,需要滚动的时候,再去测试。

【总结】

最后的相关代码是:

<code>        # TODO: support directly jump to buy cart page, no review and checkout
        # after click Review And Checkout then into buy cart
        # or directly into buy cart page
        # https://www.microsoft.com/en-us/store/buy/cart
        inBuyCartPage = WebDriverWait(driver, gCfg["waitTimeout"]).until(EC.url_contains("store/buy/cart"))
        logging.debug("inBuyCartPage=%s", inBuyCartPage)
        # if not "store/buy/cart" in driver.current_url:
        #     logging.error("Not found store/buy/cart in url for buy cart page")
        #     break
        # cartNumBtnElem = WebDriverWait(driver, gCfg["waitTimeout"]).until(EC.presence_of_element_located(
        #         (By.XPATH, '//article[contains(@class, "cart-item-container")]//div[@class="dropdown ember-view"]/div[@tabindex="-1"]/button[contains(@class, "btn cart-btn-dropdown")]')))
        itemRowElem = WebDriverWait(driver, gCfg["waitTimeout"]).until(EC.presence_of_element_located(
                (By.XPATH, '//article[contains(@class, "cart-item-container")]//div/div[contains(@class, "cart-item-details-row")]')))
        logging.debug("itemRowElem=%s", itemRowElem)
        cartNumBtnElem = itemRowElem.find_element_by_xpath('//div[@class="dropdown ember-view"]/div[@tabindex="-1"]/button[contains(@class, "btn cart-btn-dropdown")]')
        logging.debug("cartNumBtnElem=%s", cartNumBtnElem)
        logging.info("Found cart number button")
        cartNumBtnElem.click()
        logging.info("ACTION: Clicked cart number button to show select buy number")
        screenshotAndSave("msStore_clickedDropdownList.png")
        cartNumOptionElemList = itemRowElem.find_elements_by_xpath('//div[@class="dropdown ember-view"]/div[@tabindex="-1"]/ul[@class="dropdown-menu"]/li[@role="option"]')
        cartNumOptionCount = len(cartNumOptionElemList)
        logging.debug("cartNumOptionElemList=%s,cartNumOptionCount=%s", cartNumOptionElemList, cartNumOptionCount)
        # toSelectNum = toProcessInfoDict["orderNumber"]
        toSelectNum = cartNumOptionCount
        logging.debug("toSelectNum=%s", toSelectNum)
        logging.debug("cartNumOptionCount=%s, toProcessInfoDict['orderNumber']=%s",
                      cartNumOptionCount, toProcessInfoDict["orderNumber"])
        if cartNumOptionCount &gt; toProcessInfoDict["orderNumber"]:
            logging.debug("Current Cart select max number %s &gt; remain buy number %s, so only need select %s",
                          cartNumOptionCount, toProcessInfoDict["orderNumber"], toProcessInfoDict["orderNumber"])
            toSelectNum = toProcessInfoDict["orderNumber"]
            logging.debug("updated toSelectNum=%s", toSelectNum)

        # else:
        #     logging.warn("Current Cart select max number %s &lt; remain buy number %s, so omit for process next loop",
        #                   cartNumOptionCount, toProcessInfoDict["orderNumber"])
        #     break

        logging.info("Found cart number drop down list")
        toSelectIdx = toSelectNum - 1
        logging.debug("toSelectIdx=%s", toSelectIdx)
        carNumSelectElem = cartNumOptionElemList[toSelectIdx]
        logging.debug("carNumSelectElem=%s", carNumSelectElem)
        # if not carNumSelectElem.is_displayed():
        # logging.info("to select cart number element MAYBE invisible, now to scroll to visible")
        # driver.execute_script("arguments[0].scrollIntoView(true);", carNumSelectElem)
        # carNumSelectElem = cartNumOptionElemList[toSelectIdx]
        carNumSelectElem.click()
        logging.info("ACTION: Clicked cart number drop down list to select %s", toSelectNum)
        screenshotAndSave("msStore_selectCartNum.png")

        visibleCheckoutElem = WebDriverWait(driver, gCfg["waitTimeout"] * 2).until(EC.presence_of_element_located(
            (By.XPATH, '//div[@class="order-summary"]//button[contains(@class, "cli-begin-checkout")]')))
        logging.debug("visibleCheckoutElem=%s", visibleCheckoutElem)
        logging.info("Found visible checkout button")
        visibleCheckoutElem.click()
        logging.info("ACTION: Clicked visible checkout button")
        screenshotAndSave("msStore_afterClickCheckout.png")
</code>

转载请注明:在路上 » 【已解决】selenium中点击列表选项出错selenium.common.exceptions.ElementNotVisibleException Message element not visible

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
66 queries in 0.120 seconds, using 18.82MB memory