diff --git a/calendar/tests/behat/calendar.feature b/calendar/tests/behat/calendar.feature
index f1d615319ff..3062fd2062f 100644
--- a/calendar/tests/behat/calendar.feature
+++ b/calendar/tests/behat/calendar.feature
@@ -60,8 +60,8 @@ Feature: Perform basic calendar functionality
     When I am on "Course 1" course homepage
     And I follow "This month"
     And I click on "Really awesome event!" "link"
-    And "Course 1" "link" should exist in the ".modal-body" "css_element"
-    And I click on ".close" "css_element"
+    And "Course 1" "link" should exist in the "Really awesome event!" "dialogue"
+    And I click on ".close" "css_element" in the "Really awesome event!" "dialogue"
     And I log out
     And I log in as "student2"
     And I follow "This month"
diff --git a/enrol/meta/tests/behat/enrol_meta.feature b/enrol/meta/tests/behat/enrol_meta.feature
index 3a135bed5bf..8b6cf0bc941 100644
--- a/enrol/meta/tests/behat/enrol_meta.feature
+++ b/enrol/meta/tests/behat/enrol_meta.feature
@@ -114,5 +114,5 @@ Feature: Enrolments are synchronised with meta courses
     And I navigate to course participants
     # Suspended users can be unenrolled.
     When I click on "//a[@data-action='unenrol']" "xpath_element" in the "student2" "table_row"
-    And I click on "Unenrol" "button" in the "[data-region='modal']" "css_element"
+    And I click on "Unenrol" "button" in the "Unenrol" "dialogue"
     Then I should not see "Student 2" in the "participants" "table"
diff --git a/enrol/self/tests/behat/self_enrolment.feature b/enrol/self/tests/behat/self_enrolment.feature
index feb67bfa883..35ddfce7275 100644
--- a/enrol/self/tests/behat/self_enrolment.feature
+++ b/enrol/self/tests/behat/self_enrolment.feature
@@ -123,5 +123,5 @@ Feature: Users can auto-enrol themself in courses where self enrolment is allowe
     And I am on "Course 1" course homepage
     And I navigate to course participants
     When I click on "//a[@data-action='unenrol']" "xpath_element" in the "student1" "table_row"
-    And I click on "Unenrol" "button" in the "[data-region='modal']" "css_element"
+    And I click on "Unenrol" "button" in the "Unenrol" "dialogue"
     Then I should not see "Student 1" in the "participants" "table"
diff --git a/lib/behat/classes/partial_named_selector.php b/lib/behat/classes/partial_named_selector.php
index 82aa8812ba3..1e541a9cdf8 100644
--- a/lib/behat/classes/partial_named_selector.php
+++ b/lib/behat/classes/partial_named_selector.php
@@ -131,6 +131,8 @@ XPATH
         ]) = %locator%] |
 .//div[contains(concat(' ', normalize-space(@class), ' '), ' yui-dialog ') and
     normalize-space(descendant::div[@class='hd']) = %locator%]
+        |
+.//div[@data-region='modal' and descendant::*[@data-region='title'] = %locator%]
 XPATH
         , 'icon' => <<<XPATH
 .//*[contains(concat(' ', normalize-space(@class), ' '), ' icon ') and ( contains(normalize-space(@title), %locator%))]
diff --git a/message/tests/behat/delete_all_messages.feature b/message/tests/behat/delete_all_messages.feature
index 2adf5b3ca8a..25d0c08bb5e 100644
--- a/message/tests/behat/delete_all_messages.feature
+++ b/message/tests/behat/delete_all_messages.feature
@@ -19,7 +19,7 @@ Feature: Delete all messages
     And I click on "start-delete-messages" "message_area_action"
     And I click on "Delete all" "button"
     # Confirm dialogue.
-    And I click on "Delete" "button" in the ".modal-footer" "css_element"
+    And I click on "Delete" "button" in the "Confirm" "dialogue"
     # Confirm the interface is immediately updated.
     Then I should not see "User 2" in the "conversations" "message_area_region_content"
     # Confirm the changes are persisted.
diff --git a/theme/boost/tests/behat/behat_theme_boost_behat_blocks.php b/theme/boost/tests/behat/behat_theme_boost_behat_blocks.php
index 8761aa30e0e..12ab3f72e03 100644
--- a/theme/boost/tests/behat/behat_theme_boost_behat_blocks.php
+++ b/theme/boost/tests/behat/behat_theme_boost_behat_blocks.php
@@ -38,12 +38,13 @@ require_once(__DIR__ . '/../../../../blocks/tests/behat/behat_blocks.php');
 class behat_theme_boost_behat_blocks extends behat_blocks {
 
     public function i_add_the_block($blockname) {
-        $this->execute('behat_navigation::i_select_from_flat_navigation_drawer', get_string('addblock'));
+        $addblock = get_string('addblock');
+        $this->execute('behat_navigation::i_select_from_flat_navigation_drawer', $addblock);
 
         if (!$this->running_javascript()) {
             $this->execute('behat_general::i_click_on_in_the', [$blockname, 'link_exact', '#region-main', 'css_element']);
         } else {
-            $this->execute('behat_general::i_click_on_in_the', [$blockname, 'link_exact', '.modal-body', 'css_element']);
+            $this->execute('behat_general::i_click_on_in_the', [$blockname, 'link_exact', $addblock, 'dialogue']);
         }
     }
 
@@ -66,28 +67,30 @@ class behat_theme_boost_behat_blocks extends behat_blocks {
     }
 
     public function the_add_block_selector_should_contain_block($blockname) {
-        $this->execute('behat_navigation::i_select_from_flat_navigation_drawer', get_string('addblock'));
+        $addblock = get_string('addblock');
+        $this->execute('behat_navigation::i_select_from_flat_navigation_drawer', $addblock);
 
         $cancelstr = get_string('cancel');
         if (!$this->running_javascript()) {
             $this->execute('behat_general::should_exist_in_the', [$blockname, 'link_exact', '#region-main', 'css_element']);
             $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'link_exact', '#region-main', 'css_element']);
         } else {
-            $this->execute('behat_general::should_exist_in_the', [$blockname, 'link_exact', '.modal-body', 'css_element']);
-            $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'link_exact', '.modal-body', 'css_element']);
+            $this->execute('behat_general::should_exist_in_the', [$blockname, 'link_exact', $addblock, 'dialogue']);
+            $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'button', $addblock, 'dialogue']);
         }
     }
 
     public function the_add_block_selector_should_not_contain_block($blockname) {
-        $this->execute('behat_navigation::i_select_from_flat_navigation_drawer', get_string('addblock'));
+        $addblock = get_string('addblock');
+        $this->execute('behat_navigation::i_select_from_flat_navigation_drawer', $addblock);
 
         $cancelstr = get_string('cancel');
         if (!$this->running_javascript()) {
             $this->execute('behat_general::should_not_exist_in_the', [$blockname, 'link_exact', '#region-main', 'css_element']);
             $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'link_exact', '#region-main', 'css_element']);
         } else {
-            $this->execute('behat_general::should_not_exist_in_the', [$blockname, 'link_exact', '.modal-body', 'css_element']);
-            $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'link_exact', '.modal-body', 'css_element']);
+            $this->execute('behat_general::should_not_exist_in_the', [$blockname, 'link_exact', $addblock, 'dialogue']);
+            $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'button', $addblock, 'dialogue']);
         }
     }
 
diff --git a/user/tests/behat/edit_user_enrolment.feature b/user/tests/behat/edit_user_enrolment.feature
index 9c3f44b28d4..525b94b9c1e 100644
--- a/user/tests/behat/edit_user_enrolment.feature
+++ b/user/tests/behat/edit_user_enrolment.feature
@@ -45,7 +45,7 @@ Feature: Edit user enrolment
     And I am on "Course 1" course homepage
     And I navigate to course participants
     When I click on "Unenrol" "icon" in the "student1" "table_row"
-    And I click on "Unenrol" "button" in the ".modal-footer" "css_element"
+    And I click on "Unenrol" "button" in the "Unenrol" "dialogue"
     Then I should not see "Student 1" in the "participants" "table"
 
   @javascript