selenium中执行js代码、滑动窗口、上传文件

这篇博客详细介绍了如何利用Selenium Python库进行网页元素的交互,包括设置出发站和到达站、修改日期等操作。同时,展示了通过JavaScript执行滚动窗口到元素可见的多种方法,适用于网页自动化测试和数据抓取场景。
摘要由CSDN通过智能技术生成
import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

br = Chrome()

br.get('https://www.12306.cn/index/')
br.implicitly_wait(5)
s_sation = br.find_element(By.ID, 'fromStationText')
s_sation.click()
s_sation.clear()
s_sation.send_keys('上海')
s_sation.send_keys(Keys.ENTER)

e_sation = br.find_element(By.ID, 'toStationText')
e_sation.clear()
e_sation.send_keys('郑州')
e_sation.send_keys(Keys.ENTER)

# 方式1 执行js代码
# 使用js代码修改出发日期栏属性,把readOnly改为false即可输入内容
# date = br.find_element(By.ID,'train_date')
# js = '''
# ele = document.getElementById("train_date");
# ele.readOnly=false;
# '''
# # 调用方法传入js
# br.execute_script(js)
# date.clear()
# date.send_keys('2021-07-12')
# date.send_keys(Keys.ENTER)

# 方式2 往js代码中传参
# date = br.find_element(By.ID, 'train_date')
# js = 'arguments[0].readOnly=false'
# br.execute_script(js, date)  # br.execute_script(js,date,11,22) arguments[0]即date,arguments[1]即11;
# # arguments是数组,类似pytest的列表,接收后面传递的参数,可通过下标取值。
# time.sleep(2)
# date.clear()
# date.send_keys('2021-07-11')


# 方式3 js代码中直接给时间赋值
date = br.find_element(By.ID, 'train_date')
# 如果执行不成功,可将js代码分开执行,中间加强制等待时间
js = '''
arguments[0].readOnly=false;
arguments[0].value="2021-07-06";
'''
br.execute_script(js, date)

# br.find_element(By.ID,'search_one').click()

time.sleep(5)
br.quit()

滑动窗口

# 使用selenium定位元素滑动窗口到元素可见
import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By

br = Chrome()

br.get('https://www.taobao.com/')
br.implicitly_wait(5)

ele1 = br.find_element(By.XPATH, '//a[text()="手表"]')
# 窗口滚动到元素可见;如果不滚动到该位置,直接点击可能会出现错误,版本兼容等问题
res = ele1.location_once_scrolled_into_view  # 底层是个只读方法,写的是js代码去实现的,需使用属性的方式去调用,后面不能带();

time.sleep(5)
br.quit()
# 使用js代码定位元素滑动窗口到元素可见
import time
from selenium.webdriver import Chrome

br = Chrome()

br.get('http://news.baidu.com/')
br.implicitly_wait(5)

# js滑动窗口到指定元素位置的代码
js = '''
document.getElementById("change-city").scrollIntoView(true);
'''
br.execute_script(js)

time.sleep(5)
br.quit()
# 使用js代码滑动窗口
'''
window.scrollBy(0,200); 相对当前位置滑动窗口对应像素 x=0,y=200;可以传入负数
window.scrollTo(0,200); 滑动到页面指定位置;不存在负数,坐标原点就是0,0
滑动到页面最底部,由于每个页面高度不同,可以先使用js代码获取页面最大高度
document.body.scrollHeight
window.scrollTo(0,document.body.scrollHeight);
'''
import time
from selenium.webdriver import Chrome

br = Chrome()

br.get('http://news.baidu.com/')
br.implicitly_wait(5)

# js滑动窗口的代码
js = '''
window.scrollTo(0,document.body.scrollHeight);
'''
br.execute_script(js)

time.sleep(5)
br.quit()
# 滑动到下拉选择框中的元素可见
import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

br = Chrome()
br.get('https://www.sohu.com/')
br.implicitly_wait(5)
# 滑动到元素可见并点击
ele1 = br.find_element(By.XPATH,'//span[text()="选品牌"]')
ele1.location_once_scrolled_into_view
ele1.click()
# 先使用变量保存要定位的元素并定位然后滑动到元素可见;此处适合使用显式等待
location = (By.XPATH,'//a[text()="法拉利"]')
ele = br.find_element(*location)
ele.location_once_scrolled_into_view
time.sleep(3)
WebDriverWait(br,5,0.5).until(EC.visibility_of_element_located(location)).click()


time.sleep(5)
br.quit()
# 使用js代码同时打开多个窗口
# js代码打开指定窗口 window.open('');
import time
from selenium.webdriver import Chrome

br = Chrome()
br.get('https://www.baidu.com/')

# 方式1,使用js代码打开新窗口
# js = 'window.open("https://www.sohu.com/");'
# br.execute_script(js)

# 方式2,使用js代码打开新窗口,打开的窗口进行参数化
js = 'window.open(arguments[0]);'
br.execute_script(js,'https://www.sohu.com/')

time.sleep(3)
br.quit()

上传文件
文件上传一定是input元素type="file",定位到这个元素直接使用send_keys上传文件

多张图片上传操作,注意下图选择图片界面


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值