Selenium-WebDriver基本使用和API--1

---------------基本操作-------------------------------
以ff驱动创建一个实例打开页面
driver = webdriver.Chrome();
打开测试页面
driver.get( "http://baidu.com" );
---------------页面元素定位(页面元素)--------------
通过ID进行定位
<div id="coolestWidgetEvah">...</div> #页面代码
--->
element = driver.find_element_by_id("coolestWidgetEvah")
或
from selenium.webdriver.common.by import By
element = driver.find_element(by=By.ID, value="coolestWidgetEvah")#个人认为第二种比较麻烦!


通过Class Name定位
<div class="cheese"><span>Cheddar</span></div><div class="cheese"><span>Gouda</span></div>#页面代码
--->
cheeses = driver.find_elements_by_class_name("cheese")
或者
from selenium.webdriver.common.by import By
cheeses = driver.find_elements(By.CLASS_NAME, "cheese")


通过Tag Name定位
<iframe src="..."></iframe>#页面代码
--->
frame = driver.find_element_by_tag_name("iframe")
或者
from selenium.webdriver.common.by import By
frame = driver.find_element(By.TAG_NAME, "iframe")


通过Name定位
<input name="cheese" type="text"/>#页面代码
--->
cheese = driver.find_element_by_name("cheese")
或者
from selenium.webdriver.common.by import By
cheese = driver.find_element(By.NAME, "cheese")


通过Partial Link Text(局部链接文本)定位
<a href="http://www.google.com/search?q=cheese">search for cheese</a>>#页面代码
--->
cheese = driver.find_element_by_partial_link_text("cheese")
或者
from selenium.webdriver.common.by import By
cheese = driver.find_element(By.PARTIAL_LINK_TEXT, "cheese")


通过CSS定位
<div id="food"><span class="dairy">milk</span><span class="dairy aged">cheese</span></div>#页面代码
--->
cheese = driver.find_element_by_css_selector("#food span.dairy.aged")或者from selenium.webdriver.common.by import By
cheese = driver.find_element(By.CSS_SELECTOR, "#food span.dairy.aged")
#不建议使用此方法 因为在官网上提示 不通的浏览器中可能会导致显示不同的css样式
以上为基本的定位 还有一种xpath定位 下次再说.
-------------原文为selenium官方文档----------------

转载于:https://my.oschina.net/hyp3/blog/225313

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值