小编典典

使用Chrome时出现selenium“ selenium.common.exceptions.NoSuchElementException”

selenium

我正在尝试在Chrome上使用Selenium
QWOP,但我一直收到以下错误:

selenium.common.exceptions.NoSuchElementException: 
Message: no such element: Unable to locate element
{"method":"id","selector":"window1"
(Session info: chrome=63.0.3239.108
(Driver info: chromedriver=2.34.522913
(36222509aa6e819815938cbf2709b4849735537c), platform=Linux 4.10.0-42-generic x86_64)

在使用以下代码时:

    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    import time

    browser = webdriver.Chrome()
    browser.set_window_size(640, 480)
    browser.get('http://www.foddy.net/Athletics.html?webgl=true')
    browser.implicitly_wait(10)

    canvas = browser.find_element_by_id("window1")

    canvas.click()

    while (True):
        action = ActionChains(browser)
        action.move_to_element(canvas).perform()
        canvas.click()
        canvas.send_keys("q")

相同的代码在Firefox上可以完美地工作,但是由于我想使用chrome的功能以无头模式运行webgl游戏,因此我无法真正切换到Firefox。

任何解决方法可以使它正常工作?


阅读 1435

收藏
2020-06-26

共1个答案

小编典典

NoSuchElementException

selenium.common.exceptions.NoSuchElementException
通常称为NoSuchElementException

exception selenium.common.exceptions.NoSuchElementException(msg=None, screen=None, stacktrace=None)

NoSuchElementException 基本上在以下两种情况下抛出:

  • 使用时:
        webdriver.find_element_by_*("expression")
    //example : my_element = driver.find_element_by_xpath("xpath_expression")
  • 使用时:
        element.find_element_by_*("expression")
    //example : my_element = element.find_element_by_*("expression")

根据API文档,就像其他任何文档一样selenium.common.exceptionsNoSuchElementException应包含以下参数:

  • 消息,屏幕,堆栈跟踪
            raise exception_class(message, screen, stacktrace)
    selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":".//*[@id='create-portal-popup']/div[4]/div[1]/button[3]"}
      (Session info: chrome=61.0.3163.100)
      (Driver info: chromedriver=2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platform=Windows NT 10.0.10240 x86_64)

原因

NoSuchElementException 的原因可能是以下之一:

  • 您采用的 定位器策略 无法识别 HTML DOM 中的任何元素。
  • 您采用的 定位器策略 无法识别该元素,因为该元素不在浏览器的Viewport中
  • 您采用的“ 定位器策略” 可识别元素,但由于存在属性 style =“ display:none;”而不会显示。
  • 您采用的 定位器策略 不能 唯一地 标识 HTML DOM中 所需的元素,并且当前还可以找到其他一些 隐藏 / 不可见的 元素。
  • 您要查找的 WebElement<iframe>标签内。
  • webdriver的 实例看着外面的 WebElement 甚至之前的元素是中存在的/都看得到 HTML DOM

解决 NoSuchElementException 的解决方案可以是以下任一种:

  • 采用唯一标识所需 WebElement 的定位器策略。您可以借助 开发人员工具 (+ + 或)并使用 Element Inspector Ctrl`` Shift``I``F12 __

在这里,您将找到有关如何检查selenium3.6中元素的详细讨论,因为FF56不再可以使用Firebug吗?

        elem = driver.find_element_by_xpath("element_xpath")
    driver.execute_script("arguments[0].scrollIntoView();", elem)

在这里,您将找到有关使用Selenium在Python中滚动到页面顶部的详细讨论。

  • 如果元素具有属性 style =“ display:none;” ,请通过以下executeScript()方法删除属性:
        elem = driver.find_element_by_xpath("element_xpath")
    driver.execute_script("arguments[0].removeAttribute('style')", elem)
    elem.send_keys("text_to_send")
  • 要检查元素是否在 HTML<iframe>遍历中,可以通过以下两种方法之一定位相应的标记和所需的 iframe<iframe>``switchTo()
        driver.switch_to.frame("iframe_name")
    driver.switch_to.frame("iframe_id")
    driver.switch_to.frame(1) // 1 represents frame index

在这里,您可以找到有关如何选择一个html元素(无论它处于硒中的哪个帧)的详细讨论?

  • 如果该元素在 HTML DOM中 立即不 存在 /不 可见 ,则将WebDriverWait预期条件设置为适当的方法,如下所示: __

    • 等待 presentation_of_element_located

          element = WebDriverWait(driver, 20).until(expected_conditions.presence_of_element_located((By.XPATH, "element_xpath']")))
      
    • 要等待 visible_of_element_located

          element = WebDriverWait(driver, 20).until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "element_css")
      
    • 等待 element_to_be_clickable

          element = WebDriverWait(driver, 20).until(expected_conditions.element_to_be_clickable((By.LINK_TEXT, "element_link_text")))
      

这个用例

您会看到,NoSuchElementException因为 id 定位器无法唯一标识 画布
。要标识画布及其click()上的画布,您必须等待 画布 成为 画布clickable并实现此目的,您可以使用以下代码块:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//canvas[@id='window1']"))).click()
2020-06-26