GVKun编程网logo

python selenium,找出下载何时完成?(python selenium下载文件)

24

想了解pythonselenium,找出下载何时完成?的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于pythonselenium下载文件的相关问题,此外,我们还将为您介绍关于(seleni

想了解python selenium,找出下载何时完成?的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于python selenium下载文件的相关问题,此外,我们还将为您介绍关于(selenium+python)_UI 自动化 01_Mac 下 selenium 环境搭建、java+selenium, python+selenium浏览界面,滑动页面、python selenium找不到selenium库、Python Selenium,如何删除元素?的新知识。

本文目录一览:

python selenium,找出下载何时完成?(python selenium下载文件)

python selenium,找出下载何时完成?(python selenium下载文件)

我已经使用硒来启动下载。下载完成后,需要采取某些措施,是否有任何简单的方法可以确定下载何时完成?(我正在使用FireFox驱动程序)

答案1

小编典典

硒没有内置的方式来等待下载完成。


这里的一般想法是 等待文件出现在“下载”目录中

(selenium+python)_UI 自动化 01_Mac 下 selenium 环境搭建

(selenium+python)_UI 自动化 01_Mac 下 selenium 环境搭建

前言

Selenium 是一个用于 Web 网页 UI 自动化测试的开源框架,可以驱动浏览器模拟用户操作。支持多种平台 (Windows、Mac OS、Linux) 和多种浏览器 (IE、Firefox、Chrome、Safari),可以用多种语言 (Java,Ruby,Python,Perl,PHP,C#) 编写测试用例。

selenium+python 环境搭建

python 环境安装

mac 上安装 python 环境可参考:Mac 版 - python 环境配置(一):Python 下载安装

安装 selenium

打开终端 ->pip 安装(安装命令:pip3 install selenium)

安装浏览器驱动

安装 Chrome 浏览器驱动 ChromeDriver

1. 查看 Chrome 版本

设置 -> 关于 Chrome

2. 查找 Chrome 和 ChromeDriver 的对应关系

Chrome 和 ChromeDriver 的对应关系可参考:https://www.cnblogs.com/mini-monkey/p/12088721.html

3. 下载对应 ChromeDriver

ChromeDriver 下载地址:http://chromedriver.storage.googleapis.com/index.html

小编 1 中查看到的 chrome 版本为 79,故下载 ChromeDriver 79 版本

4. ChromeDriver 环境配置

进入 /usr/local/bin 目录(由于 Mac 该目录是隐藏的,所以可通过快捷键 command+shift+g 打开)

将解压的 ChromeDriver 拖拽到 bin 文件夹中

查看 ChromeDriver 版本:chromedriver --version

备注:输出 ChromeDriver 版本即安装成功

5. 编写脚本启动 Chrome 浏览器

1 # 启动Chrome
2 from selenium import webdriver
3 
4 driver = webdriver.Chrome()
5 driver.get(''http://www.baidu.com/'')  # 打开百度

运行脚本,打开百度页

 

java+selenium, python+selenium浏览界面,滑动页面

java+selenium, python+selenium浏览界面,滑动页面

用到selenium的时候,有时需要滑动屏幕,模拟浏览,这时候就可以用到下边的代码了:

首先是java的selenium滑屏代码(不知道为啥后边就乱了排版,自己调整下就好~)

private static Random random = new Random();
private void pretendScrolling(JavascriptExecutor js, int sum) throws InterruptedException {
sum--;
int step;
int length;
switch (sum) {
case 0:
step = random.nextInt(30) + 1 + 50;
length = random.nextInt(3000) + 1 + 3000;
smoothlyScrolling(js, 0, step, length, true);
smoothlyScrolling(js, 0, step, length, false);

smoothlyScrolling(js, 0, step, length, true);
smoothlyScrolling(js, length, step, 8000, true);

step = random.nextInt(20) + 1 + 80;
smoothlyScrolling(js, 0, step, 8000, false);

length = random.nextInt(2000) + 1 + 3000;
smoothlyScrolling(js, 0, step, length, true);
smoothlyScrolling(js, 0, 20, length, false);
break;
case 1:
step = random.nextInt(10) + 1 + 40;
smoothlyScrolling(js, 0, step, 6000, true);
Thread.sleep(3000);
smoothlyScrolling(js, 0, step, 6000, false);

length = random.nextInt(1500) + 1 + 1000;
step = random.nextInt(10) + 1 + 40;
Thread.sleep(3000);
smoothlyScrolling(js, 0, step, length, true);
Thread.sleep(5000);
step = random.nextInt(10) + 1 + 40;
smoothlyScrolling(js, length, step, 7000, true);
Thread.sleep(3000);

step = random.nextInt(20) + 1 + 80;
smoothlyScrolling(js, 0, step, 8000, false);
Thread.sleep(3000);
break;
case 2:
step = random.nextInt(10) + 1 + 30;
length = random.nextInt(1300) + 1 + 1000;
smoothlyScrolling(js, 0, step, length, true); Thread.sleep(8000); step = random.nextInt(10) + 1 + 20; smoothlyScrolling(js, length, step, 6000, true); Thread.sleep(3000); length = random.nextInt(1500) + 1 + 1500; step = random.nextInt(20) + 1 + 40; smoothlyScrolling(js, length, step, 6000, false); Thread.sleep(3000); step = random.nextInt(30) + 1 + 70;
smoothlyScrolling(js, length, step, 6000, true);
Thread.sleep(4000);
step = random.nextInt(20) + 1 + 30;
smoothlyScrolling(js, 0, step, 6000, false);
Thread.sleep(3000);
break;
case 3:
step = random.nextInt(10) + 1 + 30;
smoothlyScrolling(js, 0, step, 7000, true);
Thread.sleep(3000);
step = random.nextInt(30) + 1 + 20;
smoothlyScrolling(js, 0, step, 7000, false);
Thread.sleep(3000);
break;
case 4:
step = random.nextInt(10) + 1 + 50;
smoothlyScrolling(js, 0, step, 7000, true);
Thread.sleep(3000);
length = random.nextInt(1500) + 1 + 2000;
step = random.nextInt(25) + 1 + 20;
smoothlyScrolling(js, length, step, 7000, false);
Thread.sleep(3000);
step = random.nextInt(20) + 1 + 30;
smoothlyScrolling(js, length, step, 7000, true);
Thread.sleep(3000);
step = random.nextInt(20) + 1 + 40;
smoothlyScrolling(js, 0, step, 7000, false);
Thread.sleep(2000);
break;
case 5:
step = random.nextInt(20) + 1 + 30;
smoothlyScrolling(js, 0, step, 2500, true);
Thread.sleep(3000);
step = random.nextInt(30) + 1 + 40;
smoothlyScrolling(js, 0, step, 2500, false);
Thread.sleep(3000);
step = random.nextInt(20) + 1 + 30;
smoothlyScrolling(js, 0, step, 7000, true);
Thread.sleep(6000);
step = random.nextInt(20) + 1 + 30;
smoothlyScrolling(js, 0, step, 7000, false);
Thread.sleep(2000);
break;
default:
case 6:
step = random.nextInt(30) + 1 + 30;
length = random.nextInt(1500) + 1 + 2500;
smoothlyScrolling(js, 0, step, length, true);
step = random.nextInt(30) + 1 + 70;
smoothlyScrolling(js, 0, step, length, false);
step = random.nextInt(20) + 1 + 40;
smoothlyScrolling(js, 0, step, 7000, true);
step = random.nextInt(25) + 1 + 20;
smoothlyScrolling(js, 0, step, 7000, false);
Thread.sleep(3000);
step = random.nextInt(25) + 1 + 20;
length = random.nextInt(1500) + 1 + 3000;
smoothlyScrolling(js, 0, step, length, true);
Thread.sleep(3000);
step = random.nextInt(15) + 1 + 30;
smoothlyScrolling(js, 0, step, length, false);
break;
}
}
private void smoothlyScrolling(JavascriptExecutor js, int floor, int step, int celling, boolean direction) {
if (direction) {
for (int i = floor; i <= celling; i += step) {
js.executeScript("scrollTo(0," + i + ")");
}
} else {
for (int i = celling; i >= floor; i -= step) {
js.executeScript("scrollTo(0," + i + ")");
}
}
}

用法如下:



两个参数,其中js表示的是driver, sum是一个随机数,表示随机的从7中浏览方式之间选一种


再然后是在python selenium中的应用,我专门写了一个类出来存放浏览界面的代码,然后在需要用到它的地方导入,引用
import random
import time


class PretendScrolling:
@staticmethod
def pretend_scrolling(driver, sum):
step = 1
length = 1
if sum == 1:
step = random.randint(0, 30) + 1 + 50
length = random.randint(0, 3000) + 1 + 3000
smoothlyScrolling(driver, 0, step, length, True)
smoothlyScrolling(driver, 0, step, length, False)

smoothlyScrolling(driver, 0, step, length, True)
smoothlyScrolling(driver, length, step, 8000, False)
step = random.randint(0, 20)+ 1+80
smoothlyScrolling(driver, 0 ,step, 8000, False)

length = random.randint(0, 2000) + 1 + 3000
smoothlyScrolling(driver, 0 , step, length, True)
smoothlyScrolling(driver, 0 , 20, length, False)
elif sum == 2:
step = random.randint(0, 10) + 1 + 40
smoothlyScrolling(driver, 0, step, 6000, True)
time.sleep(3)
smoothlyScrolling(driver, 0, step, 6000, False)

length = random.randint(0, 1500) + 1 + 1000
step = random.randint(0, 10) + 1 + 40
time.sleep(3)
smoothlyScrolling(driver, 0, step, length, True)
time.sleep(5)
step = random.randint(0, 10)+1+40
smoothlyScrolling(driver, length, step, 7000, True)
time.sleep(3)

step = random.randint(0, 20)+1+80
smoothlyScrolling(driver, 0, step, 8000, False)
time.sleep(3)
elif sum == 3:
step = random.randint(0, 10) + 1 + 30
length = random.randint(0, 1300) + 1 + 1000
smoothlyScrolling(driver, 0, step, length, True)
time.sleep(8)
step = random.randint(0, 10) + 1 + 20
smoothlyScrolling(driver, length, step, 6000, True)
time.sleep(3)
length = random.randint(0, 1500) + 1 + 1500
step = random.randint(0, 20) + 1 + 40
smoothlyScrolling(driver, length, step, 6000, False)
time.sleep(3)
step = random.randint(0, 30) + 1 + 70
smoothlyScrolling(driver, length, step, 6000, True)
time.sleep(4)
step = random.randint(0, 20) + 1 + 30
smoothlyScrolling(driver, 0, step, 6000, False)
time.sleep(3)
elif sum == 4:
step = random.randint(0, 10) + 1 + 30
smoothlyScrolling(driver, 0, step, 7000, True)
time.sleep(3)
step = random.randint(0, 30) + 1 + 20
smoothlyScrolling(driver, 0, step, 7000, False)
time.sleep(3)
elif sum == 5:
step = random.randint(0, 10) + 1 + 50
smoothlyScrolling(driver, 0, step, 7000, True)
time.sleep(3)
length = random.randint(0, 1500) + 1 + 2000
step = random.randint(0, 25) + 1 + 20
smoothlyScrolling(driver, length, step, 7000, False)
time.sleep(3)
step = random.randint(0, 20) + 1 + 30
smoothlyScrolling(driver, length, step, 7000, True) time.sleep(3) step = random.randint(0, 20) + 1 + 40 smoothlyScrolling(driver, 0, step, 7000, False) time.sleep(2) elif sum == 6: step = random.randint(0, 20) + 1 + 30 smoothlyScrolling(driver, 0, step, 2500, True) time.sleep(3)
step = random.randint(0, 30) + 1 + 40
smoothlyScrolling(driver, 0, step, 2500, False)
time.sleep(3)
step = random.randint(0, 20) + 1 + 30
smoothlyScrolling(driver, 0, step, 7000, True)
time.sleep(6)
step = random.randint(0, 20) + 1 + 30
smoothlyScrolling(driver, 0, step, 7000, False)
time.sleep(2)
elif sum == 7:
step = random.randint(0, 30) + 1 + 30
length = random.randint(0, 1500) + 1 + 2500
smoothlyScrolling(driver, 0, step, length, True)
step = random.randint(0, 30) + 1 + 70
smoothlyScrolling(driver, 0, step, length, False)
step = random.randint(0, 20) + 1 + 40
smoothlyScrolling(driver, 0, step, 7000, True)
step = random.randint(0, 25) + 1 + 20
smoothlyScrolling(driver, 0, step, 7000, False)
time.sleep(3)
step = random.randint(0, 25) + 1 + 20
length = random.randint(0, 1500) + 1 + 3000
smoothlyScrolling(driver, 0, step, length, True)
time.sleep(3)
step = random.randint(0, 15) + 1 + 30
smoothlyScrolling(driver, 0, step, length, False)
else:
pass



#(不知道为啥会乱排版,这是另一个方法)def smoothlyScrolling(driver, floor, step, celling, direction):
if direction is True:
i = floor
while i <= celling:
i += step
jscript = "window.scrollTo(1, {height_i})".format(height_i=i)
driver.execute_script(jscript)
else:
i = celling
while i >= floor:
i -= step
jscript = "window.scrollTo(1, {height_i})".format(height_i=i)
driver.execute_script(jscript)



 







在python的其它类中引用如下:
首先先在相关类中引入滚动界面的类

 

然后调用:
以上就是selenium里边滑动界面的一组方法了,支持java和python.
 





因为图片好像没有加载出来,所以再放一下.然后也不知道为啥排版会乱,要用的话自己调整一下即可.

 



 

 


  

 

 

python selenium找不到selenium库

python selenium找不到selenium库

初学者在做python自动化时,经常会遇到找不到库情形,如下图所示

方法一: 在pycharm中,通过File->settings ->Project Interpreter

 

选择“+”号

 

安装selenium库

装完之后,项目解释器下变会出现selenium

此时代码中红色提示消失,可以正常使用

 

方法二:直接修改Project Interpreter 为python目录

Python Selenium,如何删除元素?

Python Selenium,如何删除元素?

如何解决Python Selenium,如何删除元素??

getElementByClassName不是的方法document。您将要使用

getElementsByClassName(''classname'')[0]...

但前提是您确定它是该课程中唯一的课程。

解决方法

我一直在尝试最后一小时删除元素,但没有成功。而且该元素只能通过类名来访问。我试过了:

js = "var aa=document.getElementsByClassName(''classname'')[0];aa.parentNode.removeChild(aa)"
driver.execute_script(js)

我收到未定义parentNode的错误。

那么,使用Selenium删除元素的最佳方法是什么?

今天关于python selenium,找出下载何时完成?python selenium下载文件的分享就到这里,希望大家有所收获,若想了解更多关于(selenium+python)_UI 自动化 01_Mac 下 selenium 环境搭建、java+selenium, python+selenium浏览界面,滑动页面、python selenium找不到selenium库、Python Selenium,如何删除元素?等相关知识,可以在本站进行查询。

本文标签: