Five reasons and solutions of selenium webdriver not locating elements

Source: Internet
Author: User
Tags xpath

1. Dynamic ID cannot be 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 causes no elements to be positioned

Because the element that needs to be positioned is inside a frame, sometimes the element is not located by a single Id/name/xpath
For example, the following XML source file:
<IframeId= "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" ><html> <head> <body class= "menubg"> <div id= "menu_ Node_type_0 "> <table width= "193" cellspacing= "0" cellpadding= "0" Border= "0"> <tbody> <tr> <tr> <TD id= "c_1"> <table class= " Menusub " cellspacing=" 0"  cellpadding= " Span class= "NodeValue editable" >0 "  border= "0"   align= "center" > <tbody> <tr class= "sub_menu"> <TD> <a href= "index.php?m=coupon&a=searchcouponinfo" target= "right_frame " > Password reset </a> </td> </Tr>

Could have passed
webelement element = Driver.findelement (By.linktext ("Password reset "));
To locate this element, but because the element is in theIframeId= "Left_frame "In this frame, it is necessary to locate this element by positioning the frame and then locating an element in the frame.
webelement element =driver.switchto (). FRAME ("Left_frame"). Findelement (By.linktext ("Password reset "));

3. Do not find elements in the same frame
You may encounter a column on the left side of the page belonging toLeft_frame, the right side belongs to the Right_frame case, at which point if the current
Left_frame, you cannot navigate through the ID toThe elements of the right_frame. At this point you need to switch to the default content by using the following statement
Driver.switchto (). Defaultcontent ();

For example, the current frame isLeft_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 description error
This 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 page
This need to increase the wait time, display wait time can be achieved through webdriverwait and util
For example:
Use webdriverwait and until to display a picture waiting to wait for the Welcome page to appear before doing anything else
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 Signed.applets.codebase_principal_support = True from About:config, (address bar input about:config can be set by Firefox)
The second is to add code like the following in front of the open code function: try {netscape.security.PrivilegeManager.enablePrivilege ("Universalbrowserread");} catch (e) {alert ("Permission Universalbrowserread denied.");

(Transferred from: http://www.51testing.com/html/87/300987-831171.html)

Five reasons and solutions of selenium webdriver 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.