Selenium“selenium.common.exceptions.NoSuchElementException”当使用Chrome [英] Selenium "selenium.common.exceptions.NoSuchElementException" when using Chrome
问题描述
我试图在Chrome上使用Selenium播放 QWOP ,但我继续收到以下错误:
selenium.common.exceptions.NoSuchElementException:
消息:no such element:Unable to找到元素
{method:id,selector:window1
(会话信息:chrome = 63.0.3239.108
(驱动程序信息: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时间
浏览器= 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)
canva s.click()
while(True):
action = ActionChains(browser)
action.move_to_element(canvas).perform()
canvas.click( )
canvas.send_keys(q)
但是因为我想使用chrome的功能在无头模式下运行webgl游戏,所以我无法切换到Firefox。
任何解决方法都可以实现这一功能吗?
NoSuchElementException
NoSuchElementException
被定义为:
异常selenium.common.exceptions.NoSuchElementException(msg = None,screen = None,stacktrace = None)
NoSuchElementException
基本上在以下两种情况下抛出:
-
使用时:
webdriver.find_element_by _ *(expression)
//例如:my_element = driver.find_element_by_xpath(xpath_expression)
使用时:
element.find_element_by _ *(expression)
//例如:my_element = element.find_element_by _ *(expression)
根据API Docs与其他任何 msg,screen,stacktrace selenium.common.exceptions
, NoSuchElementException
应包含以下参数:
raise exception_class(message,screen,stacktrace)
selenium.common.exceptions.NoSuchElementException:消息:no such element:Unable to locate element:{method: xpath,selector:.//*[@ id ='create-portal-popup'] / div [4] / div [1] / button [3]}
(会话信息:chrome = 61.0.3163.100)
(驱动程序信息:chromedriver = 2.32.498550(9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platfor m = Windows NT 10.0.10240 x86_64)
原因
NoSuchElementException 的原因可以是以下两种情况之一:
< iframe>
标记。 WebDriver 实例正在寻找 WebElement 甚至在元素存在/可见之前在HTML DOM 中。
解决方案
处理 NoSuchElementException 的解决方案可以是以下任一种: 采用唯一标识的定位器策略所需的 WebElement 。您可以借助开发人员工具( Ctrl + Shift + I 或 F12 )并使用元素检查器。
在这里您可以找到关于如何检查硒3.6中的元素作为萤火虫不是FF 56的更多选项? / a>
使用 execute_script()
方法滚动元素进入视图,如下所示:
elem = driver.find_element_by_xpath(element_xpath)
driver.execute_script(arguments [0] .scrollIntoView();,elem);
您可以在这里找到使用Selenium在Python中滚动页面顶部
Incase元素具有属性 style =display:none;,通过 executeScript()$ c
$ p $ elem = driver.find_element_by_xpath(element_xpath)
driver.execute_script(参数[0] .removeAttribute('style'),elem)
elem.send_keys(text_to_send);
检查元素是否位于< iframe>
遍历 HTML 以找到相应的< iframe>
标签和 switchTo()
所需的 iframe 通过以下任一方法:
驱动程序。 switch_to.frame(iframe_name)
driver.switch_to.frame(iframe_id)
driver.switch_to.frame(1)// 1表示帧索引
您可以在这里找到关于如何选择一个html元素,而不管它是在硒中的什么框架?如果元素不在 / 可见在HTML DOM中 立即引发 WebDriverWait 与
要等待 visibility_of_element_located :
element = WebDriverWait(driver,20).until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR,element_css)
li>
<$> c $ c> element = WebDriverWait(driver,20).until(expected_conditions.element_to_be_clickable((By.LINK_TEXT,element_link_text)))
This Usecase
您看到 NoSuchElementException
,因为 id 定位符不会识别ifies canvas 唯一。要确定画布和 click()
就必须等待画布为可点击
并实现你可以使用下面的代码块:
WebDriverWait(driver,20).until(EC。 element_to_be_clickable((By.XPATH,// canvas [@ id ='window1'])))。click()
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?
NoSuchElementException
selenium.common.exceptions.NoSuchElementException
popularly known as NoSuchElementException
is defined as :
exception selenium.common.exceptions.NoSuchElementException(msg=None, screen=None, stacktrace=None)
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")
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)
Reason
The reason for NoSuchElementException can be either of the following :
- 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.
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 :elem = driver.find_element_by_xpath("element_xpath") driver.execute_script("arguments[0].scrollIntoView();", elem);
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 :elem = driver.find_element_by_xpath("element_xpath") driver.execute_script("arguments[0].removeAttribute('style')", elem) elem.send_keys("text_to_send");
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 :driver.switch_to.frame("iframe_name") driver.switch_to.frame("iframe_id") driver.switch_to.frame(1) // 1 represents frame index
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 :
element = WebDriverWait(driver, 20).until(expected_conditions.presence_of_element_located((By.XPATH, "element_xpath']")))
To wait for visibility_of_element_located :
element = WebDriverWait(driver, 20).until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "element_css")
To wait for element_to_be_clickable :
element = WebDriverWait(driver, 20).until(expected_conditions.element_to_be_clickable((By.LINK_TEXT, "element_link_text")))
This Usecase
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“selenium.common.exceptions.NoSuchElementException”当使用Chrome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!