[英]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?有什么解决方法可以让它工作吗?
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.exceptions
, NoSuchElementException
应包含以下参数:
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)
The reason for NoSuchElementException can be either of the following : NoSuchElementException的原因可能是以下之一:
<iframe>
tag.<iframe>
标记内。The solution to address NoSuchElementException can be either of the following :解决NoSuchElementException的解决方案可以是以下之一:
Adopt a Locator Strategy which uniquely identifies the desired WebElement .采用唯一标识所需WebElement的定位器策略。 You can take help of the Developer Tools ( Ctrl + Shift + I or F12 ) and use Element Inspector .
您可以借助开发者工具( Ctrl + Shift + I或F12 )并使用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?在这里,您将找到有关如何检查 selenium3.6 中的元素的详细讨论, 因为萤火虫不再是 FF 56 的选项?
Use execute_script()
method to scroll the element in to view as follows :使用
execute_script()
方法滚动元素以查看如下:
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在这里,您将找到有关使用 Selenium 在 Python 中滚动到页面顶部的详细讨论
Incase element is having the attribute style="display: none;" Incase 元素具有属性style="display: none;" , remove the attribute through
executeScript()
method as follows : , 通过
executeScript()
方法删除属性如下:
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 and switchTo()
the desired iframe through either of the following methods :要检查元素是否在
<iframe>
向上遍历HTML以通过以下任一方法找到相应的<iframe>
标记和switchTo()
所需的iframe :
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?在这里你可以找到关于如何选择一个 html 元素无论它在 selenium 中的哪个框架的详细讨论? .
.
If the element is not present / visible in the HTML DOM immediately, induce WebDriverWait with expected_conditions set to proper method as follows :如果元素立即不存在/可见的HTML DOM,诱导WebDriverWait与expected_conditions设定为适当的方法如下:
To wait for presence_of_element_located :等待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 :等待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_to_be_clickable :
element = WebDriverWait(driver, 20).until(expected_conditions.element_to_be_clickable((By.LINK_TEXT, "element_link_text")))
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()
You can find Selenium 's java client based relevant discussion in:您可以在以下位置找到Selenium的基于java客户端的相关讨论:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.