Common causes and solutions of python+selenium not locating elements

Source: Internet
Author: User

When doing automated testing of Web applications, locating elements is essential, This process often encounters a situation where the element is not located (reported selenium.common.exceptions.NoSuchElementException), generally can be resolved from the following aspects: 1, Frame/iframe reason cannot locate the 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 () 2. XPath describes the cause of the error:This situation may be: 1, the XPath grammar rules writing errors, this wood has a way to complement the XPath grammar, write more, practice well. 2, the XPath level is too long, prone to stun. Solution: 1, or to improve the level of the Write XPath (LZ currently only point to simple XPath syntax, the evil complement). 2. You can use Firefox's firepath to copy the XPath path. 3, the page has not been loaded, the page on the elements of the operation:In general, you can set the wait, wait for the page to be displayed after the operation, as the principle of manual operation: 1, set the waiting time, the disadvantage is that you need to set a long waiting time, the case is more test is very slow; 2, set the waiting page of an element, such as a text, an input box can be, Once the specified element appears, you can do the operation. 3, during the debugging process, you can print out the HTML code of the page, in order to analyze. Solution: Import the time module. Import Timetime.sleep (3) 4. The dynamic ID does not locate the element:Solution: If discovery is a dynamic ID, locate it directly with XPath or otherwise.


Common causes and solutions of python+selenium not locating elements

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.