selenium.common.exceptions.NoSuchElementException positioning element Selenuim + Python and summary of Example illustrates the positioning element

 In doing automated testing of web applications, positioning elements are essential to this process is not often encounter when a positioning element (newspaper selenium.common.exceptions.NoSuchElementException), can generally be addressed from the following aspects:
1.Frame / Iframe can not locate the elements:
  This is the most common cause, we must first understand the essence of the next frame, frame is actually embedded in another page, and you can only webdriver in identifying a page, so you need to navigate to the corresponding frame, on the pages the position elements.
solution:
If you have a name or iframe id, then used directly switch_to_frame ( "name value") or switch_to_frame ( "id value"). as follows:
driver=webdriver.Firefox()
driver.get(r' http://www.126.com/')
driver.switch_to_frame ( 'the URS-iframe-X') # required to jump to the frame iframe
username=driver.find_element_by_name('email')
username.clear()
 
If no name or iframe id, then you can be positioned in the following way:
First # locate the iframe
elementi= driver.find_element_by_class_name('APP-editor-iframe')
Then the object to the positioning # switch_to_frame () method
driver.switch_to_frame(elementi) 
 
If the operation is completed, () method out of the current through the iframe switch_to.parent_content, or may also jump to a page through the outermost switch_to.default_content () method.
Another: I can go to another blog post under the understanding of html iframe tag: w3school the study notes HTML - Frame Tag
 
2.Xpath description of the error reasons:
     Since Xpath level too complicated, fallible. But this targeting method can effectively locate most of the elements, it is recommended to master.
solution:
Firefox 2.1 can use the firePath, copy xpath path. The way is easy because the level changes need to be rewritten over xpath path is not recommended, beginners can copy the path, and then try to modify it.
Write xpath level of 2.2 increase.
I can see additional detailed blog post summarizes: Selenuim + Python's summary and examples illustrate the positioning of elements
The Bowen detailed summary of the use of Xpath, multi-portfolio positioning in general can achieve positioning problems.
 
How to write test Xpath correct? Good writing Xpath path, you can copy directly to the Sohu browser firebug view html source code, through Xpath search: red box below, without error, then the write Xpath right path.
find_element_by_xpath("//input[@id='kw']")
 
 
 
3. The page has not loaded up, the operation of the elements on the page carried out:
     In general this case, you can set the operation again after waiting, waiting for page display, which is the principle of human manual operation of the same:
3.1 Setting the waiting time; the disadvantage is the need to set a longer waiting time, more test cases is very slow;
3.2 Setting an element of waiting for pages to appear, such as a text input box can be, once the specified elements appear, you can do the operation.
3.3 In the process of debugging can print out a page of html code, for analysis.
solution:
Import time module.
import time
time.sleep(3)
 
4. Dynamic elements not positioned id:
solution:
If found to be dynamic id, xpath direct positioning positioning or otherwise.
 
 
The secondary positioning, such as pop-up log
     Such as Baidu Baidu account login pop-up login box, you must first navigate to the Baidu pop-up box, and then navigate to the login user name and password.
 
复制代码
# coding=utf-8
'''
Created on 2016-7-20
@author: Jennifer
Project:登录百度账号
'''
from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.get("http://www.baidu.com/")

time.sleep(3)

#点击登录:有些name为tj_login的元素为不可见的,点击可见的那个登录按钮即可。
#否则会报:ElementNotVisibleException
element0=driver.find_elements_by_name("tj_login")
for ele0 in element0:
    if ele0.is_displayed():
        ele0.click()

#在登录弹出框,需先定位到登录弹出框
#否则会报:NoSuchElementException
element1=driver.find_element_by_class_name("tang-content")
element11=element1.find_element_by_id("TANGRAM__PSP_8__userName")
element11.clear()
element11.send_keys("登录名")

element2=element1.find_element_by_id("TANGRAM__PSP_8__password")
element2.clear()
element2.send_keys("密码")

element3=element1.find_element_by_id("TANGRAM__PSP_8__submit")
element3.click()
element3.submit()

try:
    assert "登录名" in driver.page_source
except AssertionError:
    print "登录失败"
else:
    print "登录成功"
    time.sleep(3)
finally:
    print "测试记录:已测试"
driver.close()
复制代码

补充:报:selenium.common.exceptions.ElementNotVisibleException

6.不可见元素定位

     如上百度登录代码,通过名称为tj_login查找的登录元素,有些是不可见的,所以加一个循环判断,找到可见元素(is_displayed())点击登录即可。

 

 

转载链接:https://www.cnblogs.com/jaww/p/9539031.html

 

Guess you like

Origin www.cnblogs.com/wenjing2019/p/12098445.html
Recommended