简体   繁体   English

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

[英]Selenium "selenium.common.exceptions.NoSuchElementException" when using Chrome

I'm trying to play QWOP using Selenium on Chrome but I keep getting the following error:我正在尝试在 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)

while using the following code:使用以下代码时:

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")

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.相同的代码在 Firefox 上完美运行,但是因为我想使用 chrome 的功能在无头模式下运行 webgl 游戏,所以我不能真正切换到 Firefox。

Any workarounds to get this working?有什么解决方法可以让它工作吗?

NoSuchElementException无此类元素异常

selenium.common.exceptions.NoSuchElementException popularly known as NoSuchElementException is defined as : selenium.common.exceptions.NoSuchElementException俗称NoSuchElementException定义为:

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

NoSuchElementException is basically thrown in 2 cases as follows : NoSuchElementException基本上在以下两种情况下抛出:

  • 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")

As per the API Docs just like any other selenium.common.exceptions , NoSuchElementException should contain the following parameters :根据 API 文档,就像任何其他selenium.common.exceptionsNoSuchElementException应包含以下参数:

  • 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)

Reason原因

The reason for NoSuchElementException can be either of the following : NoSuchElementException的原因可能是以下之一:

  • The Locator Strategy you have adopted doesn't identifies any element in the HTML DOM .您采用的定位器策略不识别HTML DOM中的任何元素。
  • The Locator Strategy you have adopted is unable to identify the element as it is not within the browser's Viewport .您采用的定位器策略无法识别该元素,因为它不在浏览器的Viewport 内
  • The Locator Strategy you have adopted identifies the element but is invisible due to presence of the attribute style="display: none;"您采用的定位器策略标识了该元素,但由于属性style="display: none;" 的存在而不可见 . .
  • The Locator Strategy you have adopted doesn't uniquely identifies the desired element in the HTML DOM and currently finds some other hidden / invisible element.您采用的定位器策略不会在HTML DOM 中唯一标识所需元素,并且当前会找到一些其他隐藏/不可见元素。
  • The WebElement you are trying to locate is within an <iframe> tag.您尝试定位的WebElement位于<iframe>标记内。
  • The WebDriver instance is looking out for the WebElement even before the element is present/visibile within the HTML DOM . WebDriver实例甚至在元素在HTML DOM 中存在/可见之前就在寻找WebElement

Solution解决方案

The solution to address NoSuchElementException can be either of the following :解决NoSuchElementException的解决方案可以是以下之一:


This Usecase这个用例

You are seeing NoSuchElementException because the id locator doesn't identifies the canvas uniquely.您看到NoSuchElementException是因为id定位器没有唯一标识画布 To identify the canvas and click() on it you have to wait for the canvas to be clickable and to achieve that you can use the following code block :要识别画布并在其上click() ,您必须等待画布clickable并实现这一点,您可以使用以下代码块:

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

Reference参考

You can find Selenium 's client based relevant discussion in:您可以在以下位置找到Selenium的基于客户端的相关讨论:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 selenium.common.exceptions.NoSuchElementException - selenium.common.exceptions.NoSuchElementException Selenium 中的 selenium.common.exceptions.NoSuchElementException - selenium.common.exceptions.NoSuchElementException in Selenium selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法使用带有Python的Selenium ChromeDriver Chrome找到元素 - selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element using Selenium ChromeDriver Chrome with Python selenium.common.exceptions.NoSuchElementException:消息: - selenium.common.exceptions.NoSuchElementException: Message: 如何修复selenium.common.exceptions.NoSuchElementException? - How to fix selenium.common.exceptions.NoSuchElementException? “错误:selenium.common.exceptions.NoSuchElementException:消息 - "Error: selenium.common.exceptions.NoSuchElementException: Message Selenium 错误 selenium.common.exceptions.NoSuchElementException 尝试通过 ZD22ZCFAC75B2DA745E77 访问 HTML 元素时 - Selenium error selenium.common.exceptions.NoSuchElementException when trying to access an HTML element via Xpath 使用 selenium 在文本字段中设置值。 selenium.common.exceptions.NoSuchElementException:消息:无法定位元素: - Setting a value in a text field using selenium. selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法通过 Python 使用 Selenium 定位元素错误 - selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element error using Selenium through Python selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法使用Selenium和Python定位元素 - selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element using Selenium and Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM