硒“selenium.common.exceptions.NoSuchElementException"使用 Chrome 时 [英] Selenium "selenium.common.exceptions.NoSuchElementException" when using 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 位于
标记内.
- WebDriver 实例甚至在 HTML DOM 中的元素存在/可见之前就在寻找 WebElement.
- The Locator Strategy you have adopted doesn't identifies any element in the HTML DOM.
- The Locator Strategy you have adopted is unable to identify the element as it is not within the browser's Viewport.
- The Locator Strategy you have adopted identifies the element but is invisible due to presence of the attribute 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.
- The WebElement you are trying to locate is within an
<iframe>
tag. - The WebDriver instance is looking out for the WebElement even before the element is present/visibile within the HTML DOM.
解决 NoSuchElementException 的解决方案可以是以下任一方式:
The solution to address NoSuchElementException can be either of the following :
采用 定位器策略标识所需的 WebElement.您可以借助 Developer Tools(Ctrl+Shift+I 或 F12) 并使用元素检查器.
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.
在这里你会找到关于如何检查 selenium3.6 中的元素,因为萤火虫不再适用于 FF 56?
使用execute_script()
方法滚动元素查看如下:
Use execute_script()
method to scroll the element in to view as follows :
elem = driver.find_element_by_xpath("element_xpath")
driver.execute_script("arguments[0].scrollIntoView();", elem)
这里有关于使用 Selenium 在 Python 中滚动到页面顶部
Incase 元素具有style="display: none;" 属性,通过executeScript()
方法删除该属性如下:
Incase element is having the attribute style="display: none;", remove the attribute through executeScript()
method as follows :
elem = driver.find_element_by_xpath("element_xpath")
driver.execute_script("arguments[0].removeAttribute('style')", elem)
elem.send_keys("text_to_send")
要检查元素是否在 中,向上遍历 HTML 以定位相应的
> 通过以下任一方法标记和
switchTo()
所需的 iframe :
To check if the element is within an <iframe>
traverse up the HTML to locate the respective <iframe>
tag and switchTo()
the desired iframe through either of the following methods :
driver.switch_to.frame("iframe_name")
driver.switch_to.frame("iframe_id")
driver.switch_to.frame(1) // 1 represents frame index
在这里你可以找到关于如何选择一个 html 元素,无论它在 selenium 的哪个框架中?.
Here you can find a detailed discussion on How can I select a html element no matter what frame it is in in selenium?.
如果元素在 HTML DOM 中不存在/可见,则立即引入 WebDriverWaitexpected_conditions 设置为适当的方法如下:
If the element is not present/visible in the HTML DOM immediately, induce WebDriverWait with expected_conditions set to proper method as follows :
element = WebDriverWait(driver, 20).until(expected_conditions.presence_of_element_located((By.XPATH, "element_xpath']")))
等待visibility_of_element_located :
element = WebDriverWait(driver, 20).until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "element_css")
element = WebDriverWait(driver, 20).until(expected_conditions.element_to_be_clickable((By.LINK_TEXT, "element_link_text")))
您看到 NoSuchElementException
是因为 id 定位器不能唯一标识 画布.要识别画布和 click()
你必须等待 canvas 成为 clickable
并实现你可以使用以下代码块:
You are seeing NoSuchElementException
because the id locator doesn't identifies the canvas uniquely. 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 :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//canvas[@id='window1']"))).click()
<小时>
参考
您可以找到Selenium的java 基于客户端的相关讨论:
Reference
You can find Selenium's java client based relevant discussion in:
这篇关于硒“selenium.common.exceptions.NoSuchElementException"使用 Chrome 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!