之前学习了通过插件进行录制,现在尝试自己写个python,初始界面百度
Case1.搜索关键字御坂美琴
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get("https://www.baidu.com/")
# driver.find_element_by_id("kw").send_keys("御坂美琴")
# time.sleep(6)
# driver.find_element_by_id("su").click()
# driver.find_element_by_name("wd").send_keys("御坂美琴")
driver.find_element_by_class_name("s_ipt").send_keys("御坂美琴")
# 点击确认内容
driver.find_element_by_id("su").click()
time.sleep(8)
driver.quit()
Case2.完整搜索,通过link_text找到页面中“新闻”点击进入
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get("https://www.baidu.com/")
driver.find_element_by_link_text("新闻").click()
time.sleep(8)
driver.quit()
Case3:部分关键字
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get("https://www.baidu.com/")
driver.find_element_by_partial_link_text("新闻").click()
time.sleep(8)
driver.quit()
Case4:通过xpath进行搜索
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get(“https://www.baidu.com/”)
driver.find_element_by_xpath("//*[@id=‘kw’]").send_keys(“时崎狂三”)
time.sleep(8)
driver.quit()
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200424000533580.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzkxNDI3OA==,size_16,color_FFFFFF,t_70)
4月24日补充:
Case5:利用By访问御坂美琴の百度百科
```python
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Firefox()
driver.get("https://www.baidu.com/")
tests = driver.find_element_by_id("s-bottom-layer-right").text
print(tests)
driver.find_element_by_id("kw").send_keys("御坂美琴")
driver.find_element_by_id("su").submit()
time.sleep(6)
driver.find_element(By.LINK_TEXT, "御坂美琴_百度百科").click()
time.sleep(3)
driver.quit()
Case6:智能等待implicitly_wait并获取title与url
from selenium import webdriver
import time
# 调入time 函数
browser = webdriver.Firefox()
browser.get("http://www.baidu.com")
browser.implicitly_wait(30)
# 智能等待30秒
titles = browser.title
print(titles)
browser.find_element_by_id("kw").send_keys("御坂美琴")
browser.find_element_by_id("su").click()
browser.implicitly_wait(10)
browser.find_element_by_link_text("御坂美琴_百度百科").click()
print(browser.current_url)
time.sleep(8)
browser.quit()
Case7:网页前进与后退 以及最小化窗口
# coding=utf-8
from selenium import webdriver
import time
browser = webdriver.Firefox()
# 访问百度首页
first_url = 'http://www.baidu.com'
print("now access %s" % first_url)
browser.get(first_url)
time.sleep(2)
# 访问新闻页面
second_url = 'http://news.baidu.com'
print("now access %s" % second_url)
browser.get(second_url)
time.sleep(2)
# 返回(后退)到百度首页
print("back to %s " % first_url)
browser.back()
time.sleep(1)
# 前进到新闻页
print("forward to %s" % second_url)
browser.forward()
# 最小化页面
browser.minimize_window()
time.sleep(2)
browser.quit()
Case8:滑动滚动条
# coding=utf-8
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get("http://www.baidu.com")
driver.find_element_by_id("kw").send_keys("御坂美琴")
driver.find_element_by_id("su").click()
time.sleep(3)
# 将页面滚动条拖到底部
js = "var q=document.documentElement.scrollTop=10000"
driver.execute_script(js)
time.sleep(3)
# 将滚动条移动到页面的顶部
js = "var q=document.documentElement.scrollTop=0"
driver.execute_script(js)
time.sleep(3)
driver.quit()