Skip to content

Commit 26e3e20

Browse files
If a string is passed into switch_to_frame, look up the element and then switch to it when in W3C Mode
1 parent 41ff348 commit 26e3e20

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

py/selenium/webdriver/remote/switch_to.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717

1818
from .command import Command
1919
from selenium.webdriver.common.alert import Alert
20+
from selenium.webdriver.common.by import By
21+
from selenium.common.exceptions import NoSuchElementException
22+
23+
try:
24+
str = basestring
25+
except NameError:
26+
pass
2027

2128

2229
class SwitchTo:
@@ -65,6 +72,12 @@ def frame(self, frame_reference):
6572
driver.switch_to.frame(1)
6673
driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0])
6774
"""
75+
if isinstance(frame_reference, basestring) and self._driver.w3c:
76+
try:
77+
frame_reference = self._driver.find_element(By.ID, frame_reference)
78+
except NoSuchElementException:
79+
frame_reference = self._driver.find_element(By.NAME, frame_reference)
80+
6881
self._driver.execute(Command.SWITCH_TO_FRAME, {'id': frame_reference})
6982

7083
def parent_frame(self):

py/test/selenium/webdriver/common/frame_switching_tests.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,30 +92,30 @@ def testShouldBeAbleToSwitchToAnIframeByItsIndex(self):
9292

9393
def testShouldBeAbleToSwitchToAFrameByItsName(self):
9494
self._load_page("frameset")
95-
self.driver.switch_to.frame(self.driver.find_element_by_name("fourth"))
95+
self.driver.switch_to.frame("fourth")
9696

9797
self.assertEqual(self.driver.find_element(By.TAG_NAME, "frame").get_attribute("name"), "child1")
9898

9999
def testShouldBeAbleToSwitchToAnIframeByItsName(self):
100100
self._load_page("iframes")
101-
self.driver.switch_to.frame(self.driver.find_element_by_name("iframe1-name"))
101+
self.driver.switch_to.frame("iframe1-name")
102102

103103
self.assertEqual(self.driver.find_element(By.NAME, "id-name1").get_attribute("value"), "name")
104104

105105
def testShouldBeAbleToSwitchToAFrameByItsID(self):
106106
self._load_page("frameset")
107-
self.driver.switch_to.frame(self.driver.find_element(By.ID, "fifth"))
107+
self.driver.switch_to.frame("fifth")
108108
self.assertEqual(self.driver.find_element(By.NAME, "windowOne").text, "Open new window")
109109

110110
def testShouldBeAbleToSwitchToAnIframeByItsID(self):
111111
self._load_page("iframes")
112-
self.driver.switch_to.frame(self.driver.find_element_by_id("iframe1"))
112+
self.driver.switch_to.frame("iframe1")
113113

114114
self.assertEqual(self.driver.find_element(By.NAME, "id-name1").get_attribute("value"), "name")
115115

116116
def testShouldBeAbleToSwitchToFrameWithNameContainingDot(self):
117117
self._load_page("frameset")
118-
self.driver.switch_to.frame(self.driver.find_element_by_id("sixth.iframe1"))
118+
self.driver.switch_to.frame("sixth.iframe1")
119119
self.assertTrue("Page number 3" in self.driver.find_element(By.TAG_NAME, "body").text)
120120

121121
def testShouldBeAbleToSwitchToAFrameUsingAPreviouslyLocatedWebElement(self):
@@ -146,7 +146,7 @@ def testShouldEnsureElementIsAFrameBeforeSwitching(self):
146146

147147
def testFrameSearchesShouldBeRelativeToTheCurrentlySelectedFrame(self):
148148
self._load_page("frameset")
149-
self.driver.switch_to.frame(self.driver.find_element_by_name("second"))
149+
self.driver.switch_to.frame("second")
150150
self.assertEqual(self.driver.find_element(By.ID, "pageNumber").text, "2")
151151

152152
try:
@@ -159,7 +159,7 @@ def testFrameSearchesShouldBeRelativeToTheCurrentlySelectedFrame(self):
159159
self.driver.switch_to.frame(self.driver.find_element_by_name("third"))
160160

161161
try:
162-
self.driver.switch_to.frame(self.driver.find_element_by_name("second"))
162+
self.driver.switch_to.frame("second")
163163
self.fail("Should have thrown NoSuchElementException")
164164
except NoSuchElementException:
165165
# Do nothing

0 commit comments

Comments
 (0)