Home >Backend Development >Python Tutorial >Why Does Selenium Throw a 'NoSuchElementException' When Controlling the QWOP Game in Chrome, and How Can It Be Fixed?
Selenium "NoSuchElementException" When Using Chrome
When using Selenium on Chrome to play the QWOP game, you may encounter the "selenium.common.exceptions.NoSuchElementException" error. This generally occurs when Selenium cannot find the specified element using the given locator.
Reasons for NoSuchElementException
Solution
This Usecase
In the provided code, the "selenium.common.exceptions.NoSuchElementException" occurs because the id locator ("window1") does not uniquely identify the canvas element. To resolve this, use the following modified code:
from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait browser = webdriver.Chrome() browser.set_window_size(640, 480) browser.get('http://www.foddy.net/Athletics.html?webgl=true') browser.implicitly_wait(10) canvas = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//canvas[@id='window1']"))) canvas.click() while (True): action = ActionChains(browser) action.move_to_element(canvas).perform() canvas.click() canvas.send_keys("q")
This modification ensures that Selenium waits until the canvas is clickable before attempting to interact with it.
The above is the detailed content of Why Does Selenium Throw a 'NoSuchElementException' When Controlling the QWOP Game in Chrome, and How Can It Be Fixed?. For more information, please follow other related articles on the PHP Chinese website!