selenium自动化(一)

元素定位

from selenium import webdriver
from selenium.webdriver.common.by import By

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

driver.find_element_by_id("kw")
driver.find_element_by_name("su")
driver.find_element_by_class_name("s_ipt")
driver.find_element_by_tag_name("input")
driver.find_element_by_link_text("hao123")
driver.find_element_by_partial_link_text("hao")
driver.find_element_by_xpath("//input[@id='kw']")
driver.find_element_by_css_selector(".s_ipt")
driver.find_element_by_css_selector("[type='checkbox']")

driver.find_element(By.ID,"kw")
driver.find_element(By.NAME,"wd")
driver.find_element(By.CSS_SELECTOR,"#kw")

driver.find_element("id","kw")
driver.find_element("css selector","#su")
浏览器操作
driver.set_window_size(480,800)
driver.maximize_window()
driver.back()
driver.forward()
driver.refresh()
操作元素

size = driver.find_element_by_id("kw").size
text = driver.find_element_by_id("kw").text
result = driver.find_element_by_id("kw").get_attribute("type")
ture = driver.find_element_by_id("kw").is_displayed()
鼠标、键盘、下拉列表

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select


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

element = driver.find_element_by_css_selector("[autocomplete='asd']")
ActionChains(driver).double_click(element).perform()
ActionChains(driver).context_click(element).perform()
ActionChains(driver).move_to_element(element).perform()

driver.find_element_by_css_selector("#kw").send_keys(Keys.BACK_SPACE)
driver.find_element_by_css_selector("#kw").send_keys(Keys.SPACE)
driver.find_element_by_css_selector("#kw").send_keys(Keys.CONTROL,'a')
driver.find_element_by_css_selector(".s_ipt").send_keys(Keys.ENTER)

element = driver.find_element_by_tag_name("select")
Select(element).select_by_index(0)
Select(element).select_by_value("0")
Select(element).select_by_visible_text("一周")






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值