0

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?

5
  • You have located an element that is not visible. You haven't necessarily done anything wrong. If you tried a wait and it never became visible, then it probably never will. It's possible that you are identifying the wrong element. Selenium was designed to not allow interactions with invisible elements (simulates a user). I would suggest that you test your locator in the browser dev console using $x(). Commented May 5, 2018 at 1:25
  • @JeffC Can I connact with you private message? Commented May 5, 2018 at 20:44
  • You can contact me here. Commented May 5, 2018 at 20:45
  • I can't do much with the code. The part I need is a link to the site and a description of what button you are trying to click. Commented May 5, 2018 at 20:53
  • kurnik.pl/pilka/#118 @JeffC Commented May 5, 2018 at 20:54

2 Answers 2

2

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")))
      
Sign up to request clarification or add additional context in comments.

5 Comments

When "To wait for visibility_of_element_located :" raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:
@Kuncioso Did you try to scrollIntoView() the element or check if the attribute style="display: none;" or the element is a hidden element?
@undetectedSelenium can you help to check this issue? stackoverflow.com/questions/72799696/…
@everalian You seem to have deleted the question.
@undetectedSelenium it is there pls check again.
0

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.