Kuncioso
Kuncioso

Reputation: 13

Element identified through find_element_by_xpath returns selenium.common.exceptions.ElementNotVisibleException: Message: element not visible

I have problem with click button. This button can click if enemy is on play and can't click if enemy go out start I tried with this:

try:
    element= driver.find_element_by_xpath("//button[@class='butwb']")
    if element.is_displayed():       
        print ("Element found")
    else:
        print ("Element not found")
except NoSuchElementException:
    print("No element found") 

Result:

Element not found

If I add element.click() :

selenium.common.exceptions.ElementNotVisibleException: Message: element not visible

What I do wrong?

Upvotes: 0

Views: 487

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193218

This error message...

selenium.common.exceptions.ElementNotVisibleException: Message: element not visible

...implies that the element with which you were trying to interact was not visible.

The below line of code to identify the element was successful as the element was present within the HTML DOM :

element= driver.find_element_by_xpath("//button[@class='butwb']")

At this point it is worth to mention that, presence of element implies that element is present on the DOM of a page. This does not garuntees that the element is visible(i.e. displayed) or interactable (i.e. clickable).

Visibility means that the element is not only displayed but also has a height and width that is greater than 0.

Hence is_displayed() method returns false and else{} block is executed which prints :

Element not found

Further when you invoke click() the following exception is raised :

selenium.common.exceptions.ElementNotVisibleException: Message: element not visible

Probable Reasons

  • 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 doesn't uniquely identifies the desired element in the HTML DOM and currently finds some other hidden / invisible element.

  • The Locator Strategy you have adopted identifies the element but is invisible due to presence of the attribute style="display: none;".

  • The WebElement may be present within a Modal Dialog Box and may not be visible/interactable immediately.

Solutions

  • 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 the Locator Strategy you have adopted doesn't uniquely identifies the desired element in the HTML DOM and currently locates some other hidden / invisible element as the first match the you have to change the Locator Strategy.

  • 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");
    
  • If the element is not present/visible/interactable within 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")))
      

Upvotes: 2

JeffC
JeffC

Reputation: 25664

After talking to Kuncioso and getting into a game, we found that there were two buttons that matched his locator and the first was hidden. The problem was solved using the code below to click the second button, the one he wanted.

driver.find_elements_by_xpath("//button[@class='butwb']")[1].click()

Upvotes: 0

Related Questions