Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome

0
0

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?

  • You must to post comments
0
0

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 main reason for NoSuchElementException being thrown is as follows :

  • 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 (display:none).
  • The WebElement you are trying to locate is within a (another) Frame.
  • The WebDriver instance is looking out for the WebElement even before the element’s presence/visibility within the HTML DOM.

Solution

The solution for the above mentioned reasons of throwing NoSuchElementException is as follows :

  • Adopt a Locator Strategy which uniquely identifies the intended WebElement.
  • scrollIntoView to bring the WebElement within the browser’s Viewport.
  • Incase element is invisible (display:none) remove the attribute display:none.
  • switch_to.frame() the intended Frame then try to identify the intended WebElement.
  • Induce WebDriverWait with ExpectedConditions set to elementToBeClickable.


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()
  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website