selenium相关操作(edge)

以下是基于edge的selenium

首先,需要下载edge的驱动

https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

点击x64直接下载

下载后将文件解压,找到msedgedriver.exe将其改为MicrosoftWebDriver.exe

然后将其复制到pycharm的项目目录下

1.第一种配置方法

driverfile_path = "驱动路径"#右键驱动,复制路径,绝对路径
driver=webdriver.Edge(executable_path=driverfile_path)
#这种方法会报错DeprecationWarning: executable_path has been deprecated,使用下面这种方法

2.第二种配置方法

from selenium.webdriver.edge.service import Service

s=Service("驱动路径")#驱动路径方式如上
driver=webdriver.Edge(service=s)
driver.maximize_window()#最大化窗口
driver.get("要访问的网址")

查找定位元素

from selenium.webdriver.common.by import By

find_element(by=By.LINK_TEXT,value="")#依据元素的内容进行查找
find_element(by=By.CLASS_NAME,value="")#依据类名进行查找
find_element(by=By.ID,value="")#依据id进行查找
find_element(by=By.TAG_NAME,value="")#依据标签名进行查找
find_element(by=By.NAME,value="")#依据name进行查找
#find_elements定位多个元素
#通过[]操作来获取单一的对象,可以通过for循环遍历输出,以此来判断位置

执行操作

from selenium.webdriver.common.action_chains import ActionChains


ActionChains(driver).move_to_element(login).pause(0.5).click(login)
ActionChains(driver).move_to_element(login).pause(0.5).click(login).perform()
#无perform,只点击但不执行函数行为;有perform,则会执行函数

切换到iframe

driver.switch_to.frame(0)#依据编号切换到iframe,0代表着第一个iframe

输入内容

import pyperclip
from selenium.webdriver.common.keys import Keys

password="1234567890"
pyperclip.copy(password)
text_password.send_keys(Keys.CONTROL,'v')#text_password为已定位的对象

延时操作

from time import sleep

sleep(1)#停止1秒

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
乘风的头像乘风管理团队
上一篇 2023年9月2日
下一篇 2023年9月2日

相关推荐