上面完整代码如下,最后关闭。
# -*- coding:utf-8 -*-
# pip install selenium -i 源镜像
from selenium import webdriver
import time
# 打开谷歌浏览器
driver = webdriver.Chrome()
# 使用谷歌浏览器打开百度
url = 'https://www.baidu.com'
driver.get(url)
# 设置打开的浏览器——窗口最大化
driver.maximize_window()
# 获取源代码(注意后面没括号)
response = driver.page_source
print(response)
time.sleep(5)
driver.close()
# -*- coding:utf-8 -*-
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
# 打开谷歌浏览器
driver = webdriver.Chrome()
# 使用谷歌浏览器打开百度
driver.get('https://www.baidu.com')
# 通过CLASS值定位,此处的class值是“s_ipt”
driver.find_element(By.CLASS_NAME,'s_ipt').send_keys("大家好")
# 通过XPATH语法定位“百度一下”按钮,并点击
driver.find_element(By.XPATH,'//*[@id="su"]').click()
time.sleep(5)
# 输入内容
send_keys('python')
# 清除输入框内容
clear()
# 鼠标单击
click()
# -*- coding:utf-8 -*-
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
# 打开谷歌浏览器
driver = webdriver.Chrome()
# 使用谷歌浏览器打开百度
driver.get('https://www.baidu.com')
# 定位搜索框
inputtag = driver.find_element(By.ID,"kw")
# 百度一下按钮
submittag = driver.find_element(By.ID,"su")
# 建立行为链
actions = ActionChains(driver)
# 给搜索框发送数据
actions.move_to_element(inputtag)
actions.send_keys_to_element(inputtag,'python')
# 选中提交按钮并提交
actions.move_to_element(submittag)
actions.click(submittag)
# 统一执行
actions.perform()
time.sleep(5)
click(on_element=None)——单击鼠标左键
click_and_hold(on_element=None) ——点击鼠标左键,不松开
context_click(on_element=None) ——点击鼠标右键
double_click(on_element=None)——双击鼠标左键
drag_and_drop(source, tanget)——拖拽到某个元素然后松开
key_down(value, element=None)——按下某个键盘上的键
key_up(value, element=None)——松开某个键
move_to_element(to_element)——鼠标移动到某个元素
perform() ——执行链中的所有动作
release(on_element=None) ——在某个元素位界松开鼠标左键
send_keys(*keys_to_send) ——发送某个键到当前焦点的元素
send_keys_to_element(element,*keys_to_send)——发送某个键到指定元素
drag_and_drop_by_offset(element, x,y)——把元素拖动到指定的坐标
# -*- coding:utf-8 -*-
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.get('http://sahitest.com/demo/mouseover.htm')
# 定位到显示文本框,还是用xpath方法
display = driver.find_element(By.XPATH,'//input[@value="Write on hover"]')
# 定位到隐藏文本框
hide = driver.find_element(By.XPATH,'//input[@value="Blank on hover"]')
action = ActionChains(driver)
time.sleep(3)
action.move_to_element(display).perform()
time.sleep(3)
action.move_to_element(hide).perform()
time.sleep(3)
# -*- coding:utf-8 -*-
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.get("http://sahitest.com/demo/dragDropMooTools.htm")
dragger = driver.find_element(By.XPATH, '//div[@id="dragger"]')
item1 = driver.find_element(By.XPATH,'//html/body/div[2]')
item2 = driver.find_element(By.XPATH,'//html/body/div[3]')
item3 = driver.find_element(By.XPATH,'//html/body/div[4]')
item4 = driver.find_element(By.XPATH,'//html/body/div[5]')
action = ActionChains(driver)
# 下面是直接拖拽的动作
action.drag_and_drop(dragger, item1).perform()
time.sleep(3)
# 下面是先点击目标不松开,再定位item2位置松开
action.click_and_hold(dragger).release(item2).perform()
time.sleep(3)
# 下面是先点击目标不松开,然后滑动到item3位置松开
action.click_and_hold(dragger).move_to_element(item3).release().perform()
time.sleep(3)
action.drag_and_drop(dragger, item4).perform()
time.sleep(3)
driver.quit()
# -*- coding:utf-8 -*-
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.get("http://sahitest.com/demo/clicks.htm")
# 单击按钮
click_one = driver.find_element(By.XPATH, '//input[@value="click me"]')
# 双击按钮
click_dbl = driver.find_element(By.XPATH, '//input[@value="dbl click me"]')
# 右击按钮
click_rgt = driver.find_element(By.XPATH, '//input[@value="right click me"]')
# 定义下面的一个行为链,完成单击,双击,右击
ActionChains(driver).click(click_one).double_click(click_dbl).context_click(click_rgt).perform()
time.sleep(5)
# -*- coding:utf-8 -*-
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
# 定义一个递归函数
def search():
try:
input = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "kw")))
input.send_keys("大家好")
time.sleep(10)
except TimeoutException:
# 重复进入函数尝试完成
return search()
if __name__ =='__main__':
search()