day24-selenium基本操作

day24-selenium基本操作

选项卡的切换(中国知网为例)
from selenium.webdriver import Chrome
from bs4 import BeautifulSoup
import time
b = Chrome()
# 1.打开中国知网
b.get('http://kns.cnki.net/')

# 2.输入数据分析,回车
search = b.find_element_by_id('txt_search')
search.send_keys('数据分析\n')
time.sleep(3) #停顿三秒,为了加载完,也为了程序员方便查看

# 3.获取所有论文名对应的a标签
name_a_list = b.find_elements_by_css_selector('.result-table-list .fz14')
print(len(name_a_list))

# 4.点击进入详情页
for x in name_a_list:
    # print(x)

# 点击打开新的页面
    x.click()

# 切换选项卡,让浏览器对象指向第二个页面(新页面)
# b.window_handles —— 获取当前浏览器中所有的选项卡
    b.switch_to.window(b.window_handles[-1])

    time.sleep(1)
    soup = BeautifulSoup(b.page_source,'lxml')
    try:
        print(soup.select_one('.doc-top .row').text)
    except:
        print('没有摘要')
    print('---------害羞的分割线-----------')

# 关闭第二个页面的数据
    b.close()
    b.switch_to.window(b.window_handles[0])


input('end:')
b.close()
滚动(京东为例)
from selenium.webdriver import Chrome
from bs4 import BeautifulSoup
import time

b = Chrome('chromedriver.exe')
b.get('http://www.jd.com')

b.find_element_by_id('key').send_keys('手机\n')
time.sleep(1)
for i in range(6):
    b.execute_script('window.scrollBy(0,500)') # 控制窗口滚动500像素,共滚动6次
    time.sleep(1) # 每滚动一次停一秒

soup = BeautifulSoup(b.page_source,'lxml')# 解析当前页面
goods_li = soup.select('#J_goodsList>ul>li.gl-item')# 获得当前页面的所有商品的li 标签
print(len(goods_li))#打印当前页面共有多少商品,

time.sleep(2)
b.close()
部分滚动(西瓜视频)

1)window.scrollBy(x偏移量, y偏移量) - 让整个网页直接滚动
2)js标签对象.scrollBy(x偏移量, y偏移量) - 让指定标签中的内容滚动
js获取标签的方法:
document.getElementById(id属性值) - 获取id属性值为指定值的标签,返回标签对象
document.getElementsByClassName(class属性值) - 获取class属性值为指定值的所有标签,返回一个列表,列表中的元素是标签

from selenium.webdriver import Chrome
import time

b = Chrome()
b.get('https://www.ixigua.com/?wid_try=1')
for _ in range(10):
    time.sleep(1)
    # b.execute_script('window.scrollBy(0, 800)')
    b.execute_script("document.getElementsByClassName('v3-app-layout__side__Normal')[0].scrollBy(0, 200)")

input('end:')

b.close()
等待

1.隐式等待 - 只针对通过selenium获取标签的操作
设置隐式等待时间是为了让浏览器在获取标签的时候,标签不存在不会马上报错,而是在指定的时间范围内不断尝试重新获取这个标签,直到获取到标签或者超时为止(超时没取到就会报错)。
一个浏览器对象只需要设置一次隐式等待时间,它会作用于每次获取标签的操作。

1)创建浏览器对象

b = Chrome()

2)设置浏览器的隐式等待时间

b.implicitly_wait(5)

3)打开网页

b.get('https://www.jd.com')

2.显示等待—等到某个条件成立或者不成立才执行后续

使用方法:
1)创建等待对象: WebDriverWait(浏览器对象, 超时时间)
2)添加等待条件:
等待对象.until(条件) - 等到指定条件成立,代码才接着往后执行
等待对象.until_not(条件) - 等到指定条件不成立,代码才接着往后执行

3)常用条件:
presence_of_element_located(标签) - 当指定标签存在
visibility_of_element_located(标签) - 当指定标签可见

text_to_be_present_in_element(标签, 值) - 当指定标签的标签内容中包含指定值
text_to_be_present_in_element_value(标签, 值) - 当指定标签的value属性值包含指定值的时候

注意:标签的写法:(By.获取标签方式, 数据)
例如:(By.ID, ‘key’) - id属性值为key的标签
(By.CSS_SELECTOR, ‘#p1 a’)

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

wait = WebDriverWait(b, 30)

# 等id属性为key的标签的value属性值包含'手机'为止
wait.until(EC.text_to_be_present_in_element_value((By.ID, 'key'), '手机'))

print('=====手机出现!=======')

input('end:')
b.close()
基本配置(取消图片加载,页面加载更快)
from selenium.webdriver import Chrome, ChromeOptions

# 1. 创建配置对象
options = ChromeOptions()
# 1)取消测试环境
options.add_experimental_option('excludeSwitches', ['enable-automation'])
# 2)取消图片加载
options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})

b = Chrome(options=options)

b.get('https://www.jd.com')

input('end:')
b.close()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿酱不秃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值