使用Selenium进行Web自动化操作

我们可使用Selenium进行Web自动化操作。

一、环境搭建:

1.下载安装Pycharm

2.在Pycharm中新建项目

3.在Pycharm中安装Selenium(FIle->Settings->Project:项目名->Python Interpreter->加号->搜索添加Selenium)

4.下载符合浏览器版本的chromedriver.exe

114之前chromedriver驱动版本:淘宝镜像

115以后chromedriver驱动版本:国内镜像

5.将chromedriver.exe放在项目中

 6.编写初始化代码,运行

import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

s = Service("chromedriver.exe")
browser = webdriver.Chrome(service=s)
browser.get("http://www.gushiwen.cn")

time.sleep(10)

 

二、元素定位

使用find_element或find_elements函数

1.通过ID

2.通过CLASS_NAME

3.通过TAG_NAME

4.通过CSS_SELECTOR

5.通过XPATH

6.通过LINK_TEXT

7.通过NAME

使用示例如下:

import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

s = Service("chromedriver.exe")
browser = webdriver.Chrome(service=s)
browser.get("http://www.gushiwen.cn")

input = browser.find_element(By.ID,"txtKey")
input.send_keys("李白")

inputBtn = browser.find_element(By.CSS_SELECTOR,"#search > form > input[type=submit]:nth-child(3)")
inputBtn.click()

time.sleep(10)

 

三、获取元素信息

1.xx.text

2.xx.get_attribute("yy")

3.xx.tag_name

使用示例如下:

btn = browser.find_element(By.LINK_TEXT,"古籍")
print(btn.get_attribute("href"))

 

四、与页面交互

1.xx.click()

2.xx.send_keys(yy)

3.browser.execute_script(js)

4.browser.back()

5.browser.refresh()

6.browser.quit()

使用示例如下:

import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

s = Service("chromedriver.exe")
browser = webdriver.Chrome(service=s)
browser.get("http://www.gushiwen.cn")

time.sleep(8)
browser.refresh()
time.sleep(5)

input = browser.find_element(By.ID,"txtKey")
input.send_keys("李白")

inputBtn = browser.find_element(By.CSS_SELECTOR,"#search > form > input[type=submit]:nth-child(3)")
inputBtn.click()

js = "document.documentElement.scrollTop = '10000'"
browser.execute_script(js)

time.sleep(5)
browser.back()

time.sleep(5)
browser.quit()
time.sleep(5)

 

五、与IPA工具交互

需要在项目目录加上chromedriver.exe,注意填写入参和出参。

代码示例如下:

import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

s = Service("chromedriver.exe")
browser = webdriver.Chrome(service=s)
browser.get("http://www.gushiwen.cn")

input_field = browser.find_element(By.ID,"txtKey")
input_field.send_keys(input_val)

inputBtn = browser.find_element(By.CSS_SELECTOR,"#search > form > input[type=submit]:nth-child(3)")
inputBtn.click()

res = browser.find_element(By.CSS_SELECTOR,"body > div.main3 > div.left > div.sonspic > div.cont > p:nth-child(3)")
output_val = res.text

time.sleep(5)

 

六、无界面操作

代码示例如下:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("--headless")
options.add_argument("--disable-gpu") #减少BUG
options.binary_location = r"C:\Program Files\Google\Chrome\Application\chrome.exe"

browser = webdriver.Chrome(options=options)
browser.get("http://www.gushiwen.cn")
browser.save_screenshot("gushiwen.png")

ps:在处理文件路径时,r前缀很有用。它可以确保路径中的反斜杠在字符串中保持不变,而不会被解释为转义字符。这在处理Windows文件路径时尤其有用。

 

posted @ 2023-12-17 20:09  罗毅豪  阅读(40)  评论(0编辑  收藏  举报