2

I'm running into an issue trying to click on a specific button on my web application using Selenium with Python. The button shows up on a dialog that becomes available once another button is pressed.

For example, my form has the following section:

enter image description here

Once the 'Save Answers' button is pressed, an OK button shows up in a dialog above it.

enter image description here

I'm trying to click on this OK button, but it is not working as of right now. When I inspect this element on Chrome, I get the following items:

<div class="bootbox modal fade my-modal in" tabindex="-1" role="dialog" style="display: block;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<div class="bootbox-body">Your answers have been saved.</div>
</div>
<div class="modal-footer">
<button data-bb-handler="cancel" type="button" class="btn btn btn-primary">OK</button>
</div></div></div></div>

With <button data-bb-handler="cancel" type="button" class="btn btn btn-primary">OK</button> being the button I am trying to click.

I am using the following Python code to try this:

driver.find_element_by_id("saveQuestionsSection").click() #click Save Answer
driver.find_element_by_xpath("//button[@type='button']").click() #click OK

But it is not working. I have tried to replicate this step using Selenium IDE and it works there, with the command looking like this:

Command: click

Target: xpath=(//button[@type='button'])[26]

But I am not sure how to translate this into Python code.

Any ideas?

4 Answers 4

2

Use CSS selector.

find_element_by_css_selector(".btn").click()
Sign up to request clarification or add additional context in comments.

2 Comments

How can I get the css selector id from Inspect Element in Chrome?
here are steps @FlameDra -- 1 )Hover the cursor over the element and right click mouse. 2) Select ‘Inspect Element’. 3) See the highlighted element code. 4) Right click on the highlighted code. 5) Select ‘Copy CSS Path’
1

Try to wait until modal window with OK button appeared:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver.find_element_by_id("saveQuestionsSection").click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='OK']"))).click()

4 Comments

I tried your code, but it doesn't work and gives the following error: Traceback (most recent call last): File "test.py", line 98, in <module> WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@type='button']"))).click() File "C:\Python\Python36\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:
Are you sure that "//button[@type='button']" works fine in IDE? Try with more specific XPath "//button[text()='OK']"
In IDE the target is xpath=(//button[@type='button'])[26] and it works. Not sure how to translate this to Python.
Oh, that's mean that there are at least 26 buttons with the same selector... This seem to be bad XPath :)
1

As per the HTML you have provided to click on the button with text as OK you can use the following lines of code :

//imports
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
// other lines of code
driver.find_element_by_id("saveQuestionsSection").click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='bootbox modal fade my-modal in']//div[@class='modal-footer']/button[@class='btn btn btn-primary' and contains(.,'OK')]"))).click()

Comments

1

try this :

driver.find_element_by_id("saveQuestionsSection").click() #click Save Answer
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "button[text()='OK']"))  
element.click()

OR

driver.find_element_by_id("saveQuestionsSection").click() #click Save Answer
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "(//button[@type='button'])[26]"))  
element.click()

1 Comment

@FlameDra : try both of them , it may resolve your issue.

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.