Selenium | after page Jump, you can't locate the element. How to break it?

This article is excerpted from the internal textbook of Hogwarts Testing Institute

When you want to locate an element, if you can't locate it, you should consider whether the browser has embedded a frame window or the element you want to find is in the newly opened window. At this time, frame is required
Switch between or windows.

frame is similar to nesting another HTML on the basis of the original main HTML, and the nested html is used independently and does not affect each other.

When you open a page, the cursor is positioned in the main page. If the page is composed of multiple frames, you can't directly locate the specific elements. You need to switch to the frame you need
, and then find the element.

iframe parsing

As shown in the figure, you can see the label of iframe

Various switching modes of iframe

HTML code example

        <iframe src="1.html" id="hogwarts_id" name="hogwarts_name"></iframe>

Then switch the frame by passing in the WebElement object of id, name, index and Selenium

  • index: pass in integer parameters, starting from 0, where 0 is the first frame

    • driver.switch_to.frame(0)
      ``
  • id: id of iframe

    • driver.switch_to.frame("hogwarts_id")
      ``
  • name: iframe name

    • driver.switch_to.frame("hogwarts_name")
      ``
  • WebElement: passed in selenium WebElement object

    • driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))

iframe switches back to the default page

At driver switch_ to. After frame (), if you still want to manipulate the original page, you can use

        driver.switch_to.default_content()

iframe multilayer switching

{: width="50%"}

As shown in the figure, if the multi-layer nested iframe is switched from the outermost iframe to iframe2, it needs to be switched layer by layer

      driver.switch_to.frame("iframe1")driver.switch_to.frame("iframe2")
    

Switching from iframe2 to iframe1 can use parent-child switching

        # from iframe2 Switch to the previous level iframe1 driver.switch_to.parent_frame()# Switch from iframe1 to the upper iframe. If the iframe is already the highest level, it will remain the same driver switch_ to. parent_ frame()

This method is provided by Selenium. You can directly switch from the child frame to the parent frame and use it in the nested frame framework.

Multi window processing  

The element has attributes, and the browser window also has attributes. The attributes of the browser window are identified by a handle.

When the browser opens a window, handle switching is required if you want to operate in a new window.

Get handle

When there are multiple windows, you can use window_handles print handle:

        >>> browser = webdrver.Chrome()>>> handles = browser.window_handles>>> handles

Printed window_handles:

        ['CDwindow-8012E9EF4DC788A58DC1588E7B8A7C44', 'CDwindow-11D52927C71E7C2B9984F2D1E2856049']

Handle switching

By printing the handles, you can see that it is a list, so you can use switch_to.window() to switch the handle

As can be seen from the description in the source code above, switch_to.window() needs to provide a windows_name, which can be either name or windows
handle.

        from selenium import webdriver  
            browser = webdriver.Chrome()handles = browser.window_handlesprint(handles)browser.switch_to.window(handles[-1])

The only thing to note here is that handles[-1] is a list, where - 1 represents the penultimate browser window.

Actual combat cases

    Baidu search "Hogwarts Testing Institute" and click "Hogwarts Testing Institute"_Tencent classroom ", click" intermediate and advanced test development "「Directional training of famous enterprises」class-Hogwarts Testing Institute.  


Code example:  

          *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   * 
          *     
          *     
          *     
          *     from selenium import webdriver  
          *       
          *     class TestHogwarts:    def setup_method(self, method):        self.driver = webdriver.Chrome()        self.driver.implicitly_wait(3)  
          *         def teardown_method(self, method):        self.driver.quit()  
          *         def test_hogwarts(self):        self.driver.get('https://www.baidu.com ') # in the input box, enter Hogwarts test institute self driver. find_ element_ by_ id('kw'). send_ Keys ('hogwarts Testing Institute ') # Click to search for self driver. find_ element_ by_ css_ selector('.s_btn'). click() # use link_ Textclick self driver. find_ element_ by_ link_text ('hogwarts testing institute Tencent classroom ') click() # will get the window_handles is assigned to a variable handles = self driver. window_handles # toggle handle self driver. switch_ to. window(handles[-1])        assert len(self.driver.find_elements_by_css_selector('.ag-title-main')) == 1

When doing Web automation, have you learned the positioning of frame and multi window processing in Web pages? Let's stop here today. You can also leave a message to tell us what else you want to see!

** _
Come to Hogwarts test and development society to learn more advanced technologies of software testing and test development. The knowledge points include web automated testing, app automated testing, interface automated testing, test framework, performance testing, security testing, continuous integration / continuous delivery / DevOps, test left, test right, precision testing, test platform development, test management, etc, The course technology covers bash, pytest, junit, selenium, appium, postman, requests, httprunner, jmeter, jenkins, docker, k8s, elk, sonarqube, Jacobo, JVM sandbox and other related technologies, so as to comprehensively improve the technical strength of test and development engineers
QQ communication group: 484590337
The official account TestingStudio
Video data collection: https://qrcode.testing-studio.com/f?from=CSDN&url=https://ceshiren.com/t/topic/15844
Click for more information

Keywords: Front-end Selenium html software testing Testing

Added by shivabharat on Mon, 27 Dec 2021 19:45:45 +0200