The Rookie
The Rookie

Reputation: 605

Unable to click on a popup with id's using selenium webdriver and ruby

Following is the code i am using

def self.yes_publish
      sleep 5
      driver.find_element(:id, 'dialogConfirmChanges-publishButton').displayed?
      WAIT.until { driver.find_element(:id, 'dialogConfirmChanges-publishButton') }.click        
      puts driver.find_element(:id, 'embed-left-center-part').displayed?
     end

But i am unable to click on it. This id works fine in irb. I get an error modal dialog present, as webdriver is unable to locate element it closes to window after a particular timeout. This popup is to publish changes made on a page.

xpath = .//*[@id='dialogConfirmChanges-publishButton']

Upvotes: 0

Views: 3376

Answers (1)

Arup Rakshit
Arup Rakshit

Reputation: 118271

You have to use the switch_to method to deal with the pop-up. Look at the documentation of JavaScript dialogs :

You can use webdriver to handle Javascript alert(), prompt() and confirm() dialogs. The API for all three is the same.

Note: At this time alert handling is only available in Firefox and IE (or in those browsers through the remote server), and only alerts that are generated post onload can be captured.

require "selenium-webdriver"

driver = Selenium::WebDriver.for :firefox
driver.navigate.to "http://mysite.com/page_with_alert.html"

driver.find_element(:name, 'element_with_alert_javascript').click
a = driver.switch_to.alert
if a.text == 'A value you are looking for'
  a.dismiss
else
  a.accept
end

EDIT

As per the comment box HTML given by you..I think below should work :

driver.find_element(:xpath,"//div[@class='ui-dialog-buttonset']/button[@id='dia‌​logConfirmChanges-publishButton']").click

Upvotes: 1

Related Questions