selenium元素定位
selenium提供了以下方法来定位元素
find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector
如果定位多个元素的话,可以使用以下方法(这些方法返回的结果是一个列表)
find_elements_by_name
find_elements_by_xpath
find_elements_by_link_text
find_elements_by_partial_link_text
find_elements_by_tag_name
find_elements_by_class_name
fins_elements_by_css_selector
除了以上提供的公有方法以外,在页面对象中进行元素定位还可以使用两种私有方法:find_element和find_elements
from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Chrome() driver.find_element(By.ID,'username') driver.find_elements(By.CLASS_NAME,'content')
类By可以使用的属性包括
ID = 'id' XPATH = 'xpath' LINK_TEXT = 'link text' PARTIAL_LINK_TEXT = 'partial link text' NAME = 'name' TAG_NAME = 'tag name' CLASS_NAME = 'class name' CSS_SELECTOR = 'css selector'
id元素定位
HTML
<html> <body> <form id="loginForm"> <input name="username" type="text" /> <input name="password" type="password" /> <input name="continue" type="submit" value="Login" /> </form> </body> <html>
login_form = driver.find_element_by_id('loginForm')
name元素定位
HTML
<html> <body> <form id="loginForm"> <input name="username" type="text" /> <input name="password" type="password" /> <input name="continue" type="submit" value="Login" /> <input name="continue" type="button" value="Clear" /> </form> </body> <html>
username = driver.find_element_by_name('username') password = driver.find_element_by_name('password')
xpath元素定位
<html> <body> <form id="loginForm"> <input name="username" type="text" /> <input name="password" type="password" /> <input name="continue" type="submit" value="Login" /> <input name="continue" type="button" value="Clear" /> </form> </body> <html>
定位form
login_form = driver.find_element_by_xpath("/html/body/form[1]")
login_form = driver.find_element_by_xpath("//form[1]")
login_form = driver.find_element_by_xpath("//form[@id='loginForm']")
定位username
username = driver.find_element_by_xpath("//form[input/@name='username']")
username = driver.find_element_by_xpath("//form[@id='loginForm']/input[1]")
username = driver.find_element_by_xpath("//input[@name='username']")
定位clear_button
clear_button = driver.find_element_by_xpath("//input[@name='continue'][@type='button']")
clear_button = driver.find_element_by_xpath("//form[@id='loginForm']/input[4]")
link text元素定位
<html> <body> <p>Are you sure you want to do this?</p> <a href="continue.html">Continue</a> <a href="cancel.html">Cancel</a> </body> <html>
continue_link = driver.find_element_by_link_text('Continue') continue_link = driver.find_element_by_partial_link_text('Conti')
tag name元素定位
<html> <body> <h1>Welcome</h1> <p>Site content goes here.</p> </body> <html>
heading1 = driver.find_element_by_tag_name('h1')
class name元素定位
<html> <body> <p class="content">Site content goes here.</p> </body> <html>
content = driver.find_element_by_class_name('content')
css元素定位
<html> <body> <p class="content">Site content goes here.</p> </body> <html>
content = driver.find_element_by_css_selector('p.content')
其他
定位iframe
HTML
<html> <body> <iframe id="frame1" name="frame11" scrolling="yes"> <iframe id="frame2" name="frame22" scrolling="no"> <input type="text"> </iframe> </iframe> </body> </html>
from selenium import webdriver driver = webdriver.Chrome() driver.switch_to.frame('frame1') #可以传入id/name/index/selenium的webElement对象 driver.switch_to.frame('frame2') #先定位frame1,然后定位frame2,层层切进去 driver.switch_to.parent_frame() #从子frame切回到父frame driver.switch_to.default_content() #切回到主文档
定位下拉框
from selenium.webdriver.support.select import Select from selenium import webdriver #定位select下拉框 driver = webdriver.Chrome() sel = driver.find_element_by_xpath("//select[@id='nr']") #定位下拉框 Select(sel).select_by_value('20') #定位选项中的value值 #定位下拉框的通用方式 drop_down = driver.find_element_by_id('drop_down') drop_down.find_element_by_id('value')
定位警告框
如果警告框不能通过前端工具进行定位,这个时候可以尝试使用switch_to.alert()方法
from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains driver = webdriver.Chrome() #悬停操作 link = driver.find_element_by_link_text('link') ActionChains(driver).move_to_element(link).perform() driver.find_element_by_name('link name').click() driver.switch_to.alert().accept() #接受警告框 driver.switch_to.alert().text() #返回警告框中的文字信息 driver.switch_to.alert().dismiss() #忽略警告框 driver.switch_to.alert().send_keys(keysToSend) #将文本发送至警告框