Source: Internet
Author: User


Five reasons and solutions of selenium webdriver not locating elements    1. Dynamic ID cannot be positioned to elementFor example://webelement xiexin_element = driver.findelement ( ("_mail_component_82_82"));  Webelement xiexin_element = driver.findelement (By.xpath ("//span[contains (., ' letter ')]"); ();  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 causes no elements to be positionedBecause the element that needs to be positioned is inside a frame, it is sometimes not through a single id/name/xpath or by locating 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://" >
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 frameYou may encounter a column to the left of the page belonging to the Left_frame, the right side belongs to the Right_frame case, at this time if 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")); ();  Need to switch to Right_frame Driver.switchto (). Defaultcontent (); Select quanzhong_select2 = new Select (Driver.switchto (). FRAME ("Right_frame"). Findelement ( ("Coupon_type_str"))  ); Quanzhong_select2.selectbyvisibletext ("0 hours after sale"); 4. XPath description errorThis is due to the fact that the path is not written according to the XPath rules, causing the element to not be found. 5. Click the speed too fast page is not loaded, you need to click on the elements on the pageThis need to increase the wait time, display wait time can be achieved through webdriverwait and util for example://with webdriverwait and until implementation of the display waiting for the Welcome page of the picture appears and 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 time by Thread.Sleep (5000);//wait 5 seconds This is forcing the thread to rest 6.firefox Security Strong, not allow cross-domain calls error  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 { (" Universalbrowserread ");  } catch (e) {alert ("Permission Universalbrowserread denied.");} Finally read the article on ethanol
Import;;importorg.openqa.selenium.webdriver;;;;public Classbuttondropdown {public static voidmain (String[] args ) throws Interruptedexception {Webdriver Dr = Newchromedriver (); File File = NewFile ("src/button_dropdown.html"); String FilePath = "file:///" + File.getabsolutepath (); System.out.printf ("nowaccesss%s \ n", FilePath);d r.get (FilePath); Thread.Sleep (1000);//positioning text is the drop-down menu for Watir-webdriver//Display the drop-down menu dr.findelement (By.linktext ("Info") first. Click ();( Newwebdriverwait (DR)). Until (new expectedcondition<boolean> () {public booleanapply (Webdriver D) { Returnd.findelement (By.classname ("Dropdown-menu")). isdisplayed ();}); /through UL re-level positioning dr.findelement (By.classname ("Dropdown-menu")). Findelement (By.linktext ("Watir-webdriver")). Click (); Thread.Sleep (1000); System.out.println ("Browser'll Be Close");d r.quit ();}}
And then I was positioning myself.
Public Xiaoyuanactivitypage Zipaixiuye () {driver.navigate (). Refresh (); (); Webdriverwrapper.waitpageload (driver,3);(new webdriverwait (Driver)). Until (Newexpectedcondition<boolean > () {public Boolean apply (webdriverdriver) {returndriver.findelement (By.classname ("Tfb_sub_li")). isdisplayed ();}); Driver.findelement (By.classname ("Tfb_sub_li")). Findelement (By.linktext ("Selfie show"). Click (); Returnpagefactory.initelements (This.getdriver (), xiaoyuanactivitypage.class);}


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: 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.