FlameDra
FlameDra

Reputation: 2087

Selenium - not able to click button using Python

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?

Upvotes: 2

Views: 5083

Answers (4)

cruisepandey
cruisepandey

Reputation: 29382

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

Upvotes: 1

undetected Selenium
undetected Selenium

Reputation: 193348

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

Upvotes: 1

Andersson
Andersson

Reputation: 52685

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

Upvotes: 1

Prany
Prany

Reputation: 2143

Use CSS selector.

find_element_by_css_selector(".btn").click()

Upvotes: 2

Related Questions