Skip to content Skip to sidebar Skip to footer

Selenium Webdriver Python: Element Is Not Currently Visible & Same Class Name

I am using Selenium in python to click a button in a dialog. I try to click'OK', but it keeps getting errors the buttons show 'display:block' in CSS, cause 'element is not visible

Solution 1:

If the element is invisible in the DOM like css { display: None}, {opacity: 0}... etc, , selenium will not be able to SEE it even given that you try to wait or time.sleep, instead you should use execute_script to run a JavaScript to trigger the desired event, something like this:

driver.execute_script('document.querySelector("span.ui-button-text").click();')

Solution 2:

You should wait for the element to be ready before trying to click on it. Use waitForVisible or similar to achieve that.
For example, something like this:

element = WebDriverWait(driver, 10).until(
               lambda driver : driver.find_element_by_xpath(element_xpath)
            )

If the class remains the same, then you should select that class with element_xpath. The last thing you need to determine is what other attribute designates the button as ready. Then you can wait for the specific argument like:

def find(driver):
    e = driver.find_element_by_xpath(element_xpath)
        if (e.get_attribute(some_attribute)==some_value):
            return False
        return e

element = WebDriverWait(driver, 10).until(find)

Solution 3:

Two different things can happen here.

• The selector is not explicitly returning the intended element

• Element load issue.

If both cases are true use explicit wait with a correct selector. In terms of selector I like using text bases search in such scenario. Notice I am using xpatth contains to make sure it eliminates any leading or tailing white spaces.

element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH , "//span[contains(text(),'OK')]")))

API doc here


Post a Comment for "Selenium Webdriver Python: Element Is Not Currently Visible & Same Class Name"