0

I am using Selenium in python to click a button in a dialog. I try to click"OK", but it keeps getting errors

  1. the buttons show "display:block" in CSS, cause "element is not visible" error
  2. find_element_by_xpath, but the xpath of the element keeps changing
  3. the class names are the same, how to choose the "OK" button?

Here are the code

<div<class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
 <div<class="ui-dialog-buttonset">
  <button type="button" class="large ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false">
   <span class="ui-button-text">Cancel</span>
  </button>
  <button type="button" class="orange large ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false">
   <span class="ui-button-text">OK</span>
  </button>
 </div>
</div>

Thank you:)

3 Answers 3

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

Comments

0

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)

2 Comments

I try to use 'time.sleep(5)', but the element is still invisible and the xpath keeps changing every time I open the dialog. :(
Can you share the URL in question?
0

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

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.