Five reasons and solutions of selenium webdriver not locating elements
1. Dynamic ID is not positioned to element for example://webelement xiexin_element = driver.findelement (By.id ("_mail_component_82_82")); Webelement xiexin_element = driver.findelement (By.xpath ("//span[contains (., ' letter ')]"); Xiexin_element.click (); The above section of the code commented out to locate element by ID, but the number after this ID "_mail_component_82_82" will change with each time you log in, it is not possible to accurately locate the element by ID. Therefore, it is recommended to use the relative path method of XPath to find the element.
2.iframe reason cannot locate element because the element that needs to be positioned is inside a frame, so sometimes through a separate id/name/xpath or cannot locate this element such as the following XML source file:
| <iframe id= "left_frame" scrolling= "Auto" frameborder= "0" src= "index.php?m =index&a=menu "Name=" Left_frame "noresize=" noresize "style=" Height:100%;visibility:inherit; Width:100%;z-index:1 "><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" > |
Could have passed webelement element = driver.findelement (by.linktext ("password reset")); To locate this element, but because the element is inside the frame of the iframe id= "Left_frame" It is necessary to locate this element by positioning the frame and then locating an element inside the frame webelement elements = Driver.switchto (). FRAME ("Left_frame"). Findelement (By.linktext ("password reset")); 3. Do not find elements in the same frame you may encounter the left column of the page belongs to Left_frame, the right side belongs to the Right_frame case, at this time, if you are currently in Left_frame, you cannot navigate to the Right_frame element by ID. At this point, you need to switch to the default content Driver.switchto () by using the following statement. Defaultcontent (); For example, the frame that is currently located is left_frame webelement xiaoshoumingxi_element = Driver.switchto (). FRAME ("Left_frame"). Findelement ( By.linktext ("Sales Details")); Xiaoshoumingxi_element.click (); Need to switch to Right_frame Driver.switchto (). Defaultcontent (); Select quanzhong_select2 = new Select (Driver.switchto (). FRAME ("Right_frame"). Findelement (By.id ("Coupon_type_str")) ); Quanzhong_select2.selectbyvisibletext ("0 hours after sale"); 4. xpath describes the error this is because when describing the path is not in accordance with the rules of the XPath to write the element can not be found in the case of 5. Click Speed too fast The page does not load, you need to click on the element on the page this need to add a certain waiting time, Display wait times can be achieved through webdriverwait and util such as://display with webdriverwait and until, etc.Wait for the picture of the Welcome page to appear before doing other operations webdriverwait wait = (new webdriverwait (driver,10)); Wait.until (New expectedcondition<boolean> () {public Boolean apply (Webdriver D) {Boolean loadcomplete = D.switchto (). FRAME ("Right_frame"). Findelement (By.xpath ("//center/div[@class = ' Welco ']/img"). isdisplayed (); return loadcomplete; } }); You can also estimate your own time through Thread.Sleep (5000);//wait 5 seconds This is forced thread rest 6.firefox security strong, do not allow cross-domain calls error description: uncaught exception: [Exception ... "Component returned failure code:0x80004005 (ns_error_failure) [Nsidomnshtmldocument.execcommand]" Nsresult: " 0x80004005 (ns_error_failure) "Location: Workaround: This is because Firefox security is strong and does not allow cross-domain calls. Firefox to remove XMLHttpRequest cross-domain restrictions, the first is set from about:config Signed.applets.codebase_principal_support = true; (address bar input about: Config to make Firefox settings) the second is to add code similar to the following in front of the open code function: try {netscape.security.PrivilegeManager.enablePrivilege (" Universalbrowserread "); } catch (e) {alert ("Permission Universalbrowserread denied.");}
Selenium locating failed records