Selenium是一個自動化測試工具,可以使用它來模擬用戶在網頁上的操作。在Selenium中,我們可以使用不同的方法來定位網頁上的元素。
這裡列出了一些常用的定位元素的方法:
使用ID定位元素
element = driver.find_element_by_id("element_id")
使用class定位元素
element = driver.find_element_by_class_name("element_class")
使用name定位元素element = driver.find_element_by_name("element_name")
使用tag name定位元素element = driver.find_element_by_tag_name("tag_name")
使用link text定位超链接element = driver.find_element_by_link_text("link_text")
使用partial link text定位部分超链接
element = driver.find_element_by_partial_link_text("partial_link_text")使用XPath定位元素
element = driver.find_element_by_xpath("xpath_expression")
使用CSS selector定位元素:element = driver.find_element_by_css_selector("css_selector")
這些方法可以根據元素的不同属性进行定位。在使用時,需要將Selenium的WebDriver物件命名為driver
,然後使用driver.find_element_by_xxx()
方法進行定位,其中xxx
是定位方法的名稱。
=========================实战=========================================
定位输入框
from selenium import webdriver
from time import sleep
import os
def test01():
chrome = webdriver.Chrome()
chrome.get('http://www.baidu.com')
sleep(2)
chrome.find_element_by_id('kw').send_keys('python')
sleep(2)
from selenium.webdriver.common.by import By
chrome.find_element(by = By.ID,value='su').click()
sleep(2)
chrome.quit()
if __name__=='__main__':
test01()
定位下拉框
1.html
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>Level Locate</title>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.9/dist/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<h3>Level locate</h3>
<div class="span3 col-md-3">
<div class="well">
<div class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Link1</a>
<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel" id="dropdown1" >
<li><a tabindex="-1" href="http://www.bjsxt.com">Action</a></li>
<li><a tabindex="-1" href="#">Another action</a></li>
<li><a tabindex="-1" href="#">Something else here</a></li>
<li class="divider"></li>
<li><a tabindex="-1" href="#">Separated link</a></li>
</ul>
</div>
</div>
</div>
<div class="span3 col-md-3">
<div class="well">
<div class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Link2</a>
<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel" >
<li><a tabindex="-1" href="#">Action</a></li>
<li><a tabindex="-1" href="#">Another action</a></li>
<li><a tabindex="-1" href="#">Something else here</a></li>
<li class="divider"></li>
<li><a tabindex="-1" href="#">Separated link</a></li>
</ul>
</div>
</div>
</div>
</body>
<script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.9/dist/js/bootstrap.min.js"></script></html>
main.py
from selenium import webdriver
from time import sleep
import os
def test02():
chrome = webdriver.Chrome()
file_path ='file:///'+os.path.abspath('./1/html/1.html')
chrome.get(file_path)
# 定位按钮
chrome.find_element_by_link_text('Link1').click()
menu = chrome.find_element_by_id('dropdown1').find_element_by_link_text('Action')
#移动焦点到某个元素
webdriver.ActionChains(chrome).move_to_element(menu).perform()
menu.click()
sleep(5)
chrome.quit()
if __name__=='__main__':
test02()