How to use switch_to method in Selene

Best Python code snippet using selene_python

frame_switching_tests.py

Source:frame_switching_tests.py Github

copy

Full Screen

1# Licensed to the Software Freedom Conservancy (SFC) under one2# or more contributor license agreements. See the NOTICE file3# distributed with this work for additional information4# regarding copyright ownership. The SFC licenses this file5# to you under the Apache License, Version 2.0 (the6# "License"); you may not use this file except in compliance7# with the License. You may obtain a copy of the License at8#9# http://www.apache.org/licenses/LICENSE-2.010#11# Unless required by applicable law or agreed to in writing,12# software distributed under the License is distributed on an13# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY14# KIND, either express or implied. See the License for the15# specific language governing permissions and limitations16# under the License.17import pytest18from selenium.common.exceptions import (19 NoSuchElementException,20 NoSuchFrameException,21 WebDriverException)22from selenium.webdriver.common.by import By23from selenium.webdriver.support.ui import WebDriverWait24from selenium.webdriver.support import expected_conditions as EC25# ----------------------------------------------------------------------------------------------26#27# Tests that WebDriver doesn't do anything fishy when it navigates to a page with frames.28#29# ----------------------------------------------------------------------------------------------30@pytest.fixture(autouse=True)31def restore_default_context(driver):32 yield33 driver.switch_to.default_content()34def testShouldAlwaysFocusOnTheTopMostFrameAfterANavigationEvent(driver, pages):35 pages.load("frameset.html")36 driver.find_element(By.TAG_NAME, "frameset") # Test passes if this does not throw.37def testShouldNotAutomaticallySwitchFocusToAnIFrameWhenAPageContainingThemIsLoaded(driver, pages):38 pages.load("iframes.html")39 driver.find_element(By.ID, "iframe_page_heading")40def testShouldOpenPageWithBrokenFrameset(driver, pages):41 pages.load("framesetPage3.html")42 frame1 = driver.find_element(By.ID, "first")43 driver.switch_to.frame(frame1)44 driver.switch_to.default_content()45 frame2 = driver.find_element(By.ID, "second")46 driver.switch_to.frame(frame2) # IE9 can not switch to this broken frame - it has no window.47# ----------------------------------------------------------------------------------------------48#49# Tests that WebDriver can switch to frames as expected.50#51# ----------------------------------------------------------------------------------------------52def testShouldBeAbleToSwitchToAFrameByItsIndex(driver, pages):53 pages.load("frameset.html")54 driver.switch_to.frame(1)55 assert driver.find_element(By.ID, "pageNumber").text == "2"56def testShouldBeAbleToSwitchToAnIframeByItsIndex(driver, pages):57 pages.load("iframes.html")58 driver.switch_to.frame(0)59 assert driver.find_element(By.NAME, "id-name1").get_attribute("value") == "name"60def testShouldBeAbleToSwitchToAFrameByItsName(driver, pages):61 pages.load("frameset.html")62 driver.switch_to.frame("fourth")63 assert driver.find_element(By.TAG_NAME, "frame").get_attribute("name") == "child1"64def testShouldBeAbleToSwitchToAnIframeByItsName(driver, pages):65 pages.load("iframes.html")66 driver.switch_to.frame("iframe1-name")67 assert driver.find_element(By.NAME, "id-name1").get_attribute("value") == "name"68def testShouldBeAbleToSwitchToAFrameByItsID(driver, pages):69 pages.load("frameset.html")70 driver.switch_to.frame("fifth")71 assert driver.find_element(By.NAME, "windowOne").text == "Open new window"72def testShouldBeAbleToSwitchToAnIframeByItsID(driver, pages):73 pages.load("iframes.html")74 driver.switch_to.frame("iframe1")75 assert driver.find_element(By.NAME, "id-name1").get_attribute("value") == "name"76def testShouldBeAbleToSwitchToFrameWithNameContainingDot(driver, pages):77 pages.load("frameset.html")78 driver.switch_to.frame("sixth.iframe1")79 assert "Page number 3" in driver.find_element(By.TAG_NAME, "body").text80def testShouldBeAbleToSwitchToAFrameUsingAPreviouslyLocatedWebElement(driver, pages):81 pages.load("frameset.html")82 frame = driver.find_element(By.TAG_NAME, "frame")83 driver.switch_to.frame(frame)84 assert driver.find_element(By.ID, "pageNumber").text == "1"85def testShouldBeAbleToSwitchToAnIFrameUsingAPreviouslyLocatedWebElement(driver, pages):86 pages.load("iframes.html")87 frame = driver.find_element(By.TAG_NAME, "iframe")88 driver.switch_to.frame(frame)89 element = driver.find_element(By.NAME, "id-name1")90 assert element.get_attribute("value") == "name"91def testShouldEnsureElementIsAFrameBeforeSwitching(driver, pages):92 pages.load("frameset.html")93 frame = driver.find_element(By.TAG_NAME, "frameset")94 with pytest.raises(NoSuchFrameException):95 driver.switch_to.frame(frame)96def testFrameSearchesShouldBeRelativeToTheCurrentlySelectedFrame(driver, pages):97 pages.load("frameset.html")98 driver.switch_to.frame("second")99 assert driver.find_element(By.ID, "pageNumber").text == "2"100 with pytest.raises(NoSuchElementException):101 driver.switch_to.frame(driver.find_element(By.NAME, "third"))102 driver.switch_to.default_content()103 driver.switch_to.frame(driver.find_element(By.NAME, "third"))104 with pytest.raises(NoSuchFrameException):105 driver.switch_to.frame("second")106 driver.switch_to.default_content()107 driver.switch_to.frame(driver.find_element(By.NAME, "second"))108 assert driver.find_element(By.ID, "pageNumber").text == "2"109def testShouldSelectChildFramesByChainedCalls(driver, pages):110 pages.load("frameset.html")111 driver.switch_to.frame(driver.find_element(By.NAME, "fourth"))112 driver.switch_to.frame(driver.find_element(By.NAME, "child2"))113 assert driver.find_element(By.ID, "pageNumber").text == "11"114def testShouldThrowFrameNotFoundExceptionLookingUpSubFramesWithSuperFrameNames(driver, pages):115 pages.load("frameset.html")116 driver.switch_to.frame(driver.find_element(By.NAME, "fourth"))117 with pytest.raises(NoSuchElementException):118 driver.switch_to.frame(driver.find_element(By.NAME, "second"))119def testShouldThrowAnExceptionWhenAFrameCannotBeFound(driver, pages):120 pages.load("xhtmlTest.html")121 with pytest.raises(NoSuchElementException):122 driver.switch_to.frame(driver.find_element(By.NAME, "Nothing here"))123def testShouldThrowAnExceptionWhenAFrameCannotBeFoundByIndex(driver, pages):124 pages.load("xhtmlTest.html")125 with pytest.raises(NoSuchFrameException):126 driver.switch_to.frame(27)127def testShouldBeAbleToSwitchToParentFrame(driver, pages):128 pages.load("frameset.html")129 driver.switch_to.frame(driver.find_element(By.NAME, "fourth"))130 driver.switch_to.parent_frame()131 driver.switch_to.frame(driver.find_element(By.NAME, "first"))132 assert driver.find_element(By.ID, "pageNumber").text == "1"133@pytest.mark.xfail_safari134def testShouldBeAbleToSwitchToParentFrameFromASecondLevelFrame(driver, pages):135 pages.load("frameset.html")136 driver.switch_to.frame(driver.find_element(By.NAME, "fourth"))137 driver.switch_to.frame(driver.find_element(By.NAME, "child1"))138 driver.switch_to.parent_frame()139 driver.switch_to.frame(driver.find_element(By.NAME, "child2"))140 assert driver.find_element(By.ID, "pageNumber").text == "11"141def testSwitchingToParentFrameFromDefaultContextIsNoOp(driver, pages):142 pages.load("xhtmlTest.html")143 driver.switch_to.parent_frame()144 assert driver.title == "XHTML Test Page"145def testShouldBeAbleToSwitchToParentFromAnIframe(driver, pages):146 pages.load("iframes.html")147 driver.switch_to.frame(0)148 driver.switch_to.parent_frame()149 driver.find_element(By.ID, "iframe_page_heading")150# ----------------------------------------------------------------------------------------------151#152# General frame handling behavior tests153#154# ----------------------------------------------------------------------------------------------155def testShouldContinueToReferToTheSameFrameOnceItHasBeenSelected(driver, pages):156 pages.load("frameset.html")157 driver.switch_to.frame(2)158 checkbox = driver.find_element(By.XPATH, "//input[@name='checky']")159 checkbox.click()160 checkbox.submit()161 # TODO(simon): this should not be needed, and is only here because IE's submit returns too162 # soon.163 WebDriverWait(driver, 3).until(EC.text_to_be_present_in_element((By.XPATH, '//p'), 'Success!'))164@pytest.mark.xfail_firefox(raises=WebDriverException,165 reason='https://github.com/mozilla/geckodriver/issues/610')166@pytest.mark.xfail_remote(raises=WebDriverException,167 reason='https://github.com/mozilla/geckodriver/issues/610')168@pytest.mark.xfail_safari169def testShouldFocusOnTheReplacementWhenAFrameFollowsALinkToA_TopTargetedPage(driver, pages):170 pages.load("frameset.html")171 driver.switch_to.frame(0)172 driver.find_element(By.LINK_TEXT, "top").click()173 expectedTitle = "XHTML Test Page"174 WebDriverWait(driver, 3).until(EC.title_is(expectedTitle))175 WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.ID, "only-exists-on-xhtmltest")))176def testShouldAllowAUserToSwitchFromAnIframeBackToTheMainContentOfThePage(driver, pages):177 pages.load("iframes.html")178 driver.switch_to.frame(0)179 driver.switch_to.default_content()180 driver.find_element(By.ID, "iframe_page_heading")181def testShouldAllowTheUserToSwitchToAnIFrameAndRemainFocusedOnIt(driver, pages):182 pages.load("iframes.html")183 driver.switch_to.frame(0)184 driver.find_element(By.ID, "submitButton").click()185 assert getTextOfGreetingElement(driver) == "Success!"186def getTextOfGreetingElement(driver):187 return WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.ID, "greeting"))).text188def testShouldBeAbleToClickInAFrame(driver, pages):189 pages.load("frameset.html")190 driver.switch_to.frame("third")191 # This should replace frame "third" ...192 driver.find_element(By.ID, "submitButton").click()193 # driver should still be focused on frame "third" ...194 assert getTextOfGreetingElement(driver) == "Success!"195 # Make sure it was really frame "third" which was replaced ...196 driver.switch_to.default_content()197 driver.switch_to.frame("third")198 assert getTextOfGreetingElement(driver) == "Success!"199def testShouldBeAbleToClickInAFrameThatRewritesTopWindowLocation(driver, pages):200 pages.load("click_tests/issue5237.html")201 driver.switch_to.frame(driver.find_element(By.ID, "search"))202 driver.find_element(By.ID, "submit").click()203 driver.switch_to.default_content()204 WebDriverWait(driver, 3).until(EC.title_is("Target page for issue 5237"))205def testShouldBeAbleToClickInASubFrame(driver, pages):206 pages.load("frameset.html")207 driver.switch_to.frame(driver.find_element(By.ID, "sixth"))208 driver.switch_to.frame(driver.find_element(By.ID, "iframe1"))209 # This should replace frame "iframe1" inside frame "sixth" ...210 driver.find_element(By.ID, "submitButton").click()211 # driver should still be focused on frame "iframe1" inside frame "sixth" ...212 assert getTextOfGreetingElement(driver), "Success!"213 # Make sure it was really frame "iframe1" inside frame "sixth" which was replaced ...214 driver.switch_to.default_content()215 driver.switch_to.frame(driver.find_element(By.ID, "sixth"))216 driver.switch_to.frame(driver.find_element(By.ID, "iframe1"))217 assert driver.find_element(By.ID, "greeting").text == "Success!"218def testShouldBeAbleToFindElementsInIframesByXPath(driver, pages):219 pages.load("iframes.html")220 driver.switch_to.frame(driver.find_element(By.ID, "iframe1"))221 element = driver.find_element(By.XPATH, "//*[@id = 'changeme']")222 assert element is not None223def testGetCurrentUrlReturnsTopLevelBrowsingContextUrl(driver, pages):224 pages.load("frameset.html")225 assert "frameset.html" in driver.current_url226 driver.switch_to.frame(driver.find_element(By.NAME, "second"))227 assert "frameset.html" in driver.current_url228def testGetCurrentUrlReturnsTopLevelBrowsingContextUrlForIframes(driver, pages):229 pages.load("iframes.html")230 assert "iframes.html" in driver.current_url231 driver.switch_to.frame(driver.find_element(By.ID, "iframe1"))232 assert "iframes.html" in driver.current_url233def testShouldBeAbleToSwitchToTheTopIfTheFrameIsDeletedFromUnderUs(driver, pages):234 pages.load("frame_switching_tests/deletingFrame.html")235 driver.switch_to.frame(driver.find_element(By.ID, "iframe1"))236 killIframe = driver.find_element(By.ID, "killIframe")237 killIframe.click()238 driver.switch_to.default_content()239 WebDriverWait(driver, 3).until_not(240 EC.presence_of_element_located((By.ID, "iframe1")))241 addIFrame = driver.find_element(By.ID, "addBackFrame")242 addIFrame.click()243 WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.ID, "iframe1")))244 driver.switch_to.frame(driver.find_element(By.ID, "iframe1"))245 WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.ID, "success")))246def testShouldBeAbleToSwitchToTheTopIfTheFrameIsDeletedFromUnderUsWithFrameIndex(driver, pages):247 pages.load("frame_switching_tests/deletingFrame.html")248 iframe = 0249 WebDriverWait(driver, 3).until(EC.frame_to_be_available_and_switch_to_it(iframe))250 # we should be in the frame now251 killIframe = driver.find_element(By.ID, "killIframe")252 killIframe.click()253 driver.switch_to.default_content()254 addIFrame = driver.find_element(By.ID, "addBackFrame")255 addIFrame.click()256 WebDriverWait(driver, 3).until(EC.frame_to_be_available_and_switch_to_it(iframe))257 WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.ID, "success")))258def testShouldBeAbleToSwitchToTheTopIfTheFrameIsDeletedFromUnderUsWithWebelement(driver, pages):259 pages.load("frame_switching_tests/deletingFrame.html")260 iframe = driver.find_element(By.ID, "iframe1")261 WebDriverWait(driver, 3).until(EC.frame_to_be_available_and_switch_to_it(iframe))262 # we should be in the frame now263 killIframe = driver.find_element(By.ID, "killIframe")264 killIframe.click()265 driver.switch_to.default_content()266 addIFrame = driver.find_element(By.ID, "addBackFrame")267 addIFrame.click()268 iframe = driver.find_element(By.ID, "iframe1")269 WebDriverWait(driver, 3).until(EC.frame_to_be_available_and_switch_to_it(iframe))270 WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.ID, "success")))271@pytest.mark.xfail_chrome(raises=NoSuchElementException)272@pytest.mark.xfail_chromiumedge(raises=NoSuchElementException)273@pytest.mark.xfail_firefox(raises=WebDriverException,274 reason='https://github.com/mozilla/geckodriver/issues/614')275@pytest.mark.xfail_remote(raises=WebDriverException,276 reason='https://github.com/mozilla/geckodriver/issues/614')277@pytest.mark.xfail_webkitgtk(raises=NoSuchElementException)278@pytest.mark.xfail_safari279def testShouldNotBeAbleToDoAnythingTheFrameIsDeletedFromUnderUs(driver, pages):280 pages.load("frame_switching_tests/deletingFrame.html")281 driver.switch_to.frame(driver.find_element(By.ID, "iframe1"))282 killIframe = driver.find_element(By.ID, "killIframe")283 killIframe.click()284 with pytest.raises(NoSuchFrameException):285 driver.find_element(By.ID, "killIframe").click()286def testShouldReturnWindowTitleInAFrameset(driver, pages):287 pages.load("frameset.html")288 driver.switch_to.frame(driver.find_element(By.NAME, "third"))289 assert "Unique title" == driver.title290def testJavaScriptShouldExecuteInTheContextOfTheCurrentFrame(driver, pages):291 pages.load("frameset.html")292 assert driver.execute_script("return window == window.top")293 driver.switch_to.frame(driver.find_element(By.NAME, "third"))294 assert driver.execute_script("return window != window.top")295@pytest.mark.xfail_chrome(reason="Fails on Travis")296@pytest.mark.xfail_safari297def testShouldNotSwitchMagicallyToTheTopWindow(driver, pages):298 pages.load("frame_switching_tests/bug4876.html")299 driver.switch_to.frame(0)300 WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.ID, "inputText")))301 for i in range(20):302 try:303 input = WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.ID, "inputText")))304 submit = WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.ID, "submitButton")))305 input.clear()306 import random307 input.send_keys("rand%s" % int(random.random()))308 submit.click()309 finally:310 url = driver.execute_script("return window.location.href")311 # IE6 and Chrome add "?"-symbol to the end of the URL312 if (url.endswith("?")):313 url = url[:len(url) - 1]314 assert pages.url("frame_switching_tests/bug4876_iframe.html") == url315def testGetShouldSwitchToDefaultContext(driver, pages):316 pages.load("iframes.html")317 driver.find_element(By.ID, "iframe1")318 driver.switch_to.frame(driver.find_element(By.ID, "iframe1"))319 driver.find_element(By.ID, "cheese") # Found on formPage.html but not on iframes.html.320 pages.load("iframes.html") # This must effectively switch_to.default_content(), too....

Full Screen

Full Screen

FrameOperation.py

Source:FrameOperation.py Github

copy

Full Screen

1# coding=utf-82from test.common.Common import *3class FrameOperation(object):4 """表格操作"""5 def __init__(self, driver):6 self.driver = driver7 def iframe1(self):8 self.default = self.driver.switch_to.default_content()9 self.iframe1 = self.driver.switch_to.frame('iframe1')10 def iframe0(self):11 self.default = self.driver.switch_to.default_content()12 self.iframe1 = self.driver.switch_to.frame('iframe1')13 self.iframe0 = self.driver.switch_to.frame('iframe0')14 def iframe01(self):15 #两个frame1同名16 self.parent = self.driver.switch_to.parent_frame()17 self.iframe1 = self.driver.switch_to.frame('iframe1')18 def iframeauto(self):19 self.default = self.driver.switch_to.default_content()20 self.iframe1 = self.driver.switch_to.frame('iframe1')21 self.iframeauto = self.driver.switch_to.frame('layui-layer' + get_last_id())22 def iframe2(self):23 self.default = self.driver.switch_to.default_content()24 self.iframe1 = self.driver.switch_to.frame('iframe1')25 self.iframeauto = self.driver.switch_to.frame('layui-layer-iframe2')26 def iframe3(self):27 self.default = self.driver.switch_to.default_content()28 self.iframe1 = self.driver.switch_to.frame('iframe1')29 self.iframeauto = self.driver.switch_to.frame('layui-layer-iframe3')30 def iframe4(self):31 self.parent = self.driver.switch_to.parent_frame()32 self.iframeauto = self.driver.switch_to.frame('layui-layer-iframe4')33 def iframe5(self):34 self.parent = self.driver.switch_to.parent_frame()35 self.iframeauto = self.driver.switch_to.frame('layui-layer-iframe5')36 def iframe6(self):37 self.parent = self.driver.switch_to.parent_frame()38 self.iframeauto = self.driver.switch_to.frame('layui-layer-iframe6')39 def parentframe(self):40 self.parent = self.driver.switch_to.parent_frame()41 def iframe00(self):42 self.iframe0 = self.driver.switch_to.frame('iframe0')43 def iframeauto01(self):44 self.iframeauto = self.driver.switch_to.frame('layui-layer-iframe' + get_last_id())45 def iframeauto_player(self):...

Full Screen

Full Screen

AlertHandle.py

Source:AlertHandle.py Github

copy

Full Screen

1from selenium import webdriver2from selenium.webdriver.firefox.service import Service3from webdriver_manager.firefox import GeckoDriverManager4from selenium.webdriver.common.by import By5driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()))6driver.get("https://the-internet.herokuapp.com/javascript_alerts")7# Normal Alert8Normal_alert = driver.find_element(By.XPATH, '//*[@id="content"]/div/ul/li[1]/button')9Normal_alert.click()10title_normalAlert = driver.switch_to.alert.text11print('Normal Alert Title is: ' + title_normalAlert)12driver.switch_to.alert.accept() # Ok13# Confirmation Alert14Normal_alert = driver.find_element(By.XPATH, '//*[@id="content"]/div/ul/li[2]/button')15Normal_alert.click()16title_confirmAlert = driver.switch_to.alert.text17print('Confirm Alert Title is: ' + title_confirmAlert)18driver.switch_to.alert.dismiss() # Cancel19# Prompt Alert20Normal_alert = driver.find_element(By.XPATH, '//*[@id="content"]/div/ul/li[3]/button')21Normal_alert.click()22title_promptAlert = driver.switch_to.alert.text23print('Prompt Alert Title is: ' + title_promptAlert)24driver.switch_to.alert.send_keys('Test Automation')25driver.switch_to.alert.accept() # Ok...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Selene automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful