
爬取时间:2021/01/27
系统环境:Windows 10
所用工具:Jupyter Notebook\Python 30
涉及的库:selenium\time\pandas\matplotlib\jieba\stylecloud
蛋肥想法: 借助selenium,实现对“查看更多”的自动点击,目标是获取2020年的文章相关数据。
蛋肥想法: 36氪的数据很满足强迫症,没有空格换行,只需筛选出2020年的数据保存。
蛋肥想法: 此次重点是学习selenium,所以只简单做一下数据可视化。
笔记:
01设置元素等待时间
自动化测试的时候,元素定位没有错,但是跑脚本的时候却报错了
例如,登录的时候要等登录页面加载出来才能输入用户名和密码
selenium设置元素等待时间的3种方式及区别
1sleep() --固定等待时间:强制等待,不能把握准确的等等时间,适合调试时用
2implicitly_wait() --隐式等待时间:等页面加载完成才执行下一步 *** 作,一般设置一次即可
3WebDriverWait --显示等待时间:配合untill()和not untill()方法,根据判断条件 灵活处理等待时间
要先设置一个超长时间,在这个时间内,程序根据调用频率每隔几秒查看一下,如果条件满足,则执行下一步 *** 作,若不满足且超过了等待时间则抛出异常
导包:from seleniumwebdriversupportui import WebDriverWait
使用:WebDriverWait(driver, 超时时间, 调用频率, 要忽略的异常)until(要执行的方法, 超时时返回的错误信息)
实例:
replace:
02键盘事件Keys类
制表符:send_keys(KeysTAB)
退出键:send_keys(KeysESCAPE)
F1:send_keys(KeysF1) f1~f12 以此类推
03二次定位
顾名思义,通过多个元素,定位两次
drfind_element(ByCLASS_NAME,"s_input")send_keys("python")
二次定位:
drfind_element(ByNAME,"query")find_element(ByCLASS_NAME,"s_input")send_keys("python")
04selenium鼠标悬停类-- ActionChains()
导包:from seleniumwebdrivercommonaction_chainsimport ActionChains
实例:将鼠标移动至更多设置,悬停鼠标
elem1= drfind_element(ByLINK_TEXT,"设置")
ActionChains(dr)move_to_element(elem1)perform()
05selenium警告框的处理
很多web应用经常会遇到JavaScript编写的alert 、confirm 以及prompt d窗,需要用到switch_toalert来切换d窗,并对d窗进行输入信息,关闭等处理。
switch_toalerttext --获取d窗文本信息
switch_toalertaccept() --点击d窗中确定按钮
switch_toalertdismiss() --点击d出中取消按钮
switch_toalertsendKeys("字符串") --在d窗中输入信息
实例:
06下拉菜单选择的三种方式
导包:from seleniumwebdriversupportuiimport Select
#选择下拉框的o2
elem1 = drfind_element(ByID,"s3Id")
1:根据value值定位
Select(elem1)select_by_value("o2val")
2根据index定位
Select(elem1)select_by_index(1)
3根据下拉框文本信息定位
Select(elem1)select_by_visible_text("o2")
07多窗口切换
window_handles --获取所有窗口句柄
switch_towindow(指定窗口) --切换到指定窗口
08selenium之JS *** 作浏览器滚动条位置
execute_script("windowscrollTo(0,n);")
09selenium之JS *** 作隐藏元素
隐藏元素的标识:style="display:none;"
一般我们通过将隐藏元素的属性修改成显示再定位
10 *** 作excel文件
将测试数据参数化用到
101准备参数化数据文件
102导包
打开文件,填写文档路径
运行结果:
定位到sheet的位置
读取sheet的行和列的内容
#读取表的行的方法
rows = sheet1nrows
print("行数",rows)
columns = sheet1ncols
print("列数",columns)
读取指定行的值
#读取指定行的值
rowValue1 = sheet1row_values(0)
print(rowValue1)
#读取指定列的值
colValue1 = sheet1col_values(0)
print(colValue1)
#输出所有用户名和对应的密码
for iin range(1,rows):
print("用户名:" + sheet1row_values(i)[0] +",密码:" + sheet1row_values(i)[1])
1 下载必要依赖文件selenium-server-standalone-2250jar, junit-47jar,并将它们放置到工程的lib文件夹下面 (我这里使用Firefox浏览器来作为客户端,所以就不需要下载额外的浏览器执行器,如果你想用IE或是Chrome做客户端,请下载对应的执行器
2 建立一个测试工程,在工程里创建一个测试文件,并添加如下代码:
import comthoughtworksseleniumSelenium;
import junitframeworkTestCase;
import orgjunitAfter;
import orgjunitBefore;
import orgjunitTest;
import orgjunitrunnerRunWith;
import orgjunitrunnersBlockJUnit4ClassRunner;
import orgopenqaseleniumBy;
import orgopenqaseleniumWebDriver;
import orgopenqaseleniumWebDriverBackedSelenium;
import orgopenqaseleniumWebElement;
import orgopenqaseleniumfirefoxFirefoxDriver;
import orgopenqaseleniuminternalWrapsDriver;
import orgopenqaseleniumsupportuiWait;
import orgopenqaseleniumsupportuiWebDriverWait;
import javaioIOException;
import static orgopenqaseleniumsupportuiExpectedConditionsvisibilityOfElementLocated;
@RunWith(BlockJUnit4ClassRunnerclass)
public class pickTest extends TestCase {
protected static Selenium selenium;
private static WebDriver driver;
@Before
public void createAndStartService() throws IOException {
selenium = new WebDriverBackedSelenium(new FirefoxDriver(), "");
driver = ((WrapsDriver) selenium)getWrappedDriver();
}
@After
public void createAndStopService() {
driverquit();
}
@Test
public void should_open_google_page() throws InterruptedException {
driverget(">
<span style="color: #ff0000;">WebElement searchBox = driverfindElement(Byxpath("//[@id=\"lst-ib\"]"));</span>
searchBoxsendKeys("selenium");
WebElement searchButton = driverfindElement(Byxpath("//[@id=\"tsf\"]/div[2]/div[3]/center/input[1]"));
searchButtonclick();
<span style="color: #3366ff;">Wait<WebDriver> wait = new WebDriverWait(driver, 30);
waituntil(visibilityOfElementLocated(Byxpath("//[@id=\"ab_name\"]/span")));</span>
}
}
3 运行这个测试,你将看到firebox浏览器被自动启动,然后会自动的输入selenum并搜索。
这样,一个简单的自动化页面测试就完成了。有的朋友可能不太明白这段代码的含义。上面的代码中我标出了红色和蓝色两部分,我简单解释一下。Selenium是通过对浏览器的包装来进行页面处理的,因此我们首先会创建一个与浏览器相关的WebDriver对象。然后我们需要查找页面元素就是通过findeElement的方法和XPath的方式来获取页面对象(红色部分代码)。那么通常我们的一个点击 *** 作产生服务器相应,这里就需要一些时间。蓝色部分的代码就是创建一个等待对象,你可以通过XPath的方式来确定返回后页面上的哪个元素加载完了就认为页面加载完了,同时等待对象也有一个超时设置,这样即是服务器端一直不返回或出错。我们依然可以结束测试。如何更快的确定页面元素的XPath,如下:
以上就是关于Python爬虫实战(3)selenium完成瀑布流数据爬取全部的内容,包括:Python爬虫实战(3)selenium完成瀑布流数据爬取、selenium之webdriver详解——小白进阶之路(二)、如何利用selenium来进行自动化页面测试等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)