硒“selenium.common.exceptions.NoSuchElementException”使用 Chrome 时
我尝试在 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。
有什么解决方法可以使其正常工作吗?
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)
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.
Any workarounds to get this working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NoSuchElementException
selenium.common.exceptions.NoSuchElementException 通常称为
NoSuchElementException
定义为:NoSuchElementException
基本上在以下两种情况下抛出:使用时:
使用时:
根据 API 文档,就像任何其他
selenium.common.exceptions
一样,NoSuchElementException
应包含以下参数:消息、屏幕、堆栈跟踪
Reason
NoSuchElementException 的原因可能是以下任一原因:
标记内。
解决方案
解决NoSuchElementException的解决方案可以是以下任一:
采用定位器策略,唯一标识所需的WebElement。您可以借助开发人员工具(Ctrl+Shift+I 或 F12)并使用元素检查器。
在这里您将找到有关如何检查 selenium3.6 中的元素,因为 firebug 不再是 FF 的选项56?
使用
execute_script()
方法滚动元素以查看如下:在这里您将找到有关使用 Selenium 在 Python 中滚动到页面顶部
如果元素具有属性 style="display: none;",请通过以下方式删除该属性
executeScript()
方法如下:要检查元素是否在
内,请遍历 HTML 以找到相应的
标记和
switchTo()
所需的iframe 通过以下任一方法:在这里您可以找到有关如何选择一个html元素,无论它在selenium中的哪个框架中?。
如果该元素不立即在 HTML DOM 中存在/可见,则引入 WebDriverWait 与 expected_conditions 设置为正确的方法如下:
等待presence_of_element_ located:
等待visibility_of_element_ located:
等待element_to_be_clickable:
这个用例
您看到的
NoSuchElementException
因为 id 定位器无法唯一标识canvas。要识别画布并对其进行click()
操作,您必须等待 canvas 变为可点击
,并且可以使用以下命令代码块:参考
你可以找到Selenium 的 java 基于客户端的相关讨论:
NoSuchElementException
selenium.common.exceptions.NoSuchElementException popularly known as
NoSuchElementException
is defined as :NoSuchElementException
is basically thrown in 2 cases as follows :When using :
When using :
As per the API Docs just like any other
selenium.common.exceptions
,NoSuchElementException
should contain the following parameters :msg, screen, stacktrace
Reason
The reason for NoSuchElementException can be either of the following :
<iframe>
tag.Solution
The solution to address NoSuchElementException can be either of the following :
Adopt a Locator Strategy which uniquely identifies the desired WebElement. You can take help of the Developer Tools (Ctrl+Shift+I or F12) and use Element Inspector.
Here you will find a detailed discussion on how to inspect element in selenium3.6 as firebug is not an option any more for FF 56?
Use
execute_script()
method to scroll the element in to view as follows :Here you will find a detailed discussion on Scrolling to top of the page in Python using Selenium
Incase element is having the attribute style="display: none;", remove the attribute through
executeScript()
method as follows :To check if the element is within an
<iframe>
traverse up the HTML to locate the respective<iframe>
tag andswitchTo()
the desired iframe through either of the following methods :Here you can find a detailed discussion on How can I select a html element no matter what frame it is in in selenium?.
If the element is not present/visible in the HTML DOM immediately, induce WebDriverWait with expected_conditions set to proper method as follows :
To wait for presence_of_element_located :
To wait for visibility_of_element_located :
To wait for element_to_be_clickable :
This Usecase
You are seeing
NoSuchElementException
because the id locator doesn't identifies the canvas uniquely. To identify the canvas andclick()
on it you have to wait for the canvas to beclickable
and to achieve that you can use the following code block :Reference
You can find Selenium's java client based relevant discussion in: