Common causes of failure of webdriver locating elements in Selenium

Source: Internet
Author: User

In automated tests, there are often situations where elements cannot be located and reported selenium.common.exceptions.NoSuchElementException errors

Frame/iframe reason cannot locate element:

This is the most common reason, first of all to understand the nature of the next frame, frame is actually embedded in another page, and Webdriver can only be recognized in a page at a time, so you need to locate the corresponding frame, the element in the page to locate. Solution: If the IFRAME has a name or ID, use Switch_to_frame ("Name value") or Switch_to_frame ("id value") directly. as follows: Driver=webdriver. Firefox () driver.get (R ' http://www.126.com/') Driver.switch_to_frame(' X-urs-iframe ') #需先跳转到iframe框架username =driver.find_element_by_name (' email ') username.clear () If the IFRAME does not have a name or ID , you can locate it in the following way: #先定位到iframeelementi = Driver.find_element_by_class_name (' app-editor-iframe ') #再将定位对象传给switch_to_ Frame () method driver.switch_to_frame(elementi)If you complete the operation, you can jump out of the current IFRAME through the Switch_to.parent_content () method, or you can skip back to the outermost page through the Switch_to.default_content () method. Another: can go to my other blog to understand the HTML of the IFRAME tag: w3school HTML Learning Notes-frame tags today try to login NetEase mailbox Test switch multiple browser windows, using a variety of ways to locate the registration button is a failure. Find the source code to find that the original landing box is a built-in IFRAME framework, no wonder there will be errors, so first locate the IFRAME and then the operation can normally click the Register button
1 #Coding:utf-82 3  fromSeleniumImportWebdriver4 Import Time5Driver =Webdriver. Firefox ()6Driver.get ('http://mail.163.com/')7 8 #get the current 163 mailbox window9Nowhandle =Driver.current_window_handleTenDriver.implicitly_wait (30) OneDriver.switch_to_frame ('X-urs-iframe')#an IFRAME is embedded in it A  - #Open the registration page -DRIVER.FIND_ELEMENT_BY_ID ('Changepage'). Click () the #Get all Windows -All_handles =Driver.window_handles -  - #Traverse handles to determine whether it is the current window +  forHandleinchAll_handles: -     ifHandle! =Nowhandle: + Driver.switch_to_window (handle) A        Print 'Registration Window' at        #driver.find_element_by_id (' Changepage '). Click () -        #driver.close () #关闭注册页 -  - #back to the original window -Time.sleep (10) -Driver.switch_to_window (Nowhandle)

Common causes of failure of webdriver locating elements in Selenium

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.