硒“selenium.common.exceptions.NoSuchElementException"使用 Chrome 时 [英] Selenium "selenium.common.exceptions.NoSuchElementException" when using Chrome

查看:34
本文介绍了硒“selenium.common.exceptions.NoSuchElementException"使用 Chrome 时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to play QWOP using Selenium on Chrome but I keep getting the following error:

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.

The same code works perfectly on Firefox, but because I want to use chrome's capability to run an webgl game in headless mode I can't really switch to Firefox.

是否有任何解决方法可以使其正常工作?

Any workarounds to get this working?

推荐答案

NoSuchElementException

selenium.common.exceptions.NoSuchElementException 俗称 NoSuchElementException 定义为:

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

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

NoSuchElementException is basically thrown in 2 cases as follows :

  • 使用时:

  • When using :

webdriver.find_element_by_*("expression")
//example : my_element = driver.find_element_by_xpath("xpath_expression")

  • 使用时:

  • When using :

    element.find_element_by_*("expression")
    //example : my_element = element.find_element_by_*("expression")
    

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

    As per the API Docs just like any other selenium.common.exceptions, NoSuchElementException should contain the following parameters :

    • 味精、屏幕、堆栈跟踪

    • msg, screen, stacktrace

        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 的原因可能是以下之一:

    The reason for NoSuchElementException can be either of the following :

    • 您采用的定位器策略未识别HTML DOM.
    • 您采用的定位器策略无法识别该元素,因为它不在浏览器的视口.
    • 您采用的定位器策略可识别元素,但由于存在属性style="display: none;"而无法显示.
    • 您采用的定位器策略不能唯一地识别HTML DOM中所需的元素,并且当前会找到其他一些隐藏/不可见元素.
    • 您尝试定位的 WebElement 位于