python selenium进阶总结

1.前进后退和切换选项卡

from selenium.webdriver import Chrome
import time

'''
b = Chrome()

b.get('https://www.baidu.com')
time.sleep(1)

b.get('https://www.runoob.com')
time.sleep(1)

b.get('https://movie.douban.com/top250')
time.sleep(1)

b.back()
time.sleep(1)

b.forward()
time.sleep(5)

b.close()
'''

b = Chrome()
douban_url = 'https://movie.douban.com/'
b.get(douban_url)
music = b.find_element_by_css_selector('.global-nav-items>ul>li:nth-child(4)>a')

music_url = music.get_attribute('href')

music.click()
time.sleep(2)
# 切换选项卡
b.switch_to.window(b.window_handles[0])
b.get(douban_url)  # 切换完选项卡以后必须重新加载这个选项卡中的页面

time.sleep(2)
b.switch_to.window(b.window_handles[1])
b.get(music_url)

b.close()

2.等待

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

b = Chrome()
b.get('https://read.douban.com/')

"""1.隐式等待 - 全局有效(只需要设置一次)
如果设置了隐式等待时间,那么浏览器对象在通过find_element相关方法获取标签的时候
在找不到对应的标签的时候不会马上报错,而是在指定时间内不断尝试获取该标签,
如果超过了指定的时间还是获取不到才会报错
"""

b.implicitly_wait(2)   # 隐式等待

"""
2.显示等待
1)创建等待对象WebDriverWait(浏览器对象,超时时间)
2)
等待对象.until(条件) - 等到指定条件为True获取对应标签
等待对象.until_not(条件) - 等到指定条件为False获取对应标签


常见的条件:
EC.presence_of_element_located((By.标签获取方式,获取方式值)):判断某个元素是否被加到dom树里(判断某个标签是否加载到网页中,不一定可见)
EC.visibility_of_element_located:判断某个标签是否可见(没有隐藏,并且元素的宽度和高度都不等于0)
EC.text_to_be_present_in_element:判断某个标签中的标签内容是否包含了预期的字符串
EC.text_to_be_present_in_element_value:判断某个标签中的value属性是否包含了预期的字符串
EC.element_to_be_clickable:判断某个标签是否可以点击
"""

wait = WebDriverWait(b, 5)
wait.until(EC.presence_of_element_located((By.ID, 'p')))
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'css选择器')))

wait.until(EC.visibility_of_element_located((By.ID, 'id属性值')))
wait.until(EC.text_to_be_present_in_element((By.ID, 'id属性值'), '请登录'))
wait.until_not(EC.text_to_be_present_in_element((By.ID, 'id属性值'), '请登录'))

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'css选择器')))
from selenium.webdriver import Chrome, ChromeOptions
from selenium.webdriver.common.keys import Keys
import time

options = ChromeOptions()
options.add_experimental_option("excludeSwitches", ['enable-automation', 'enable-logging'])
options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})

b = Chrome(options=options)
b.get('https://www.jd.com')
b.implicitly_wait(2)

search_input = b.find_element_by_id('key')
search_input.send_keys('电脑')
search_input.send_keys(Keys.ENTER)

time.sleep(2)


# b.execute_script("""
#         max_height = 4000
#         height = 0
#         timmer = setInterval(function(){
#             window.scrollTo(0, height)
#             height += 100
#             if (height > max_height){
#                 clearInterval(timmer)
#             }
#             console.log(height)
#             }, 1000)
#
#
# """)

# 滚动
max_height = 8900
height = 500
while True:
    b.execute_script(f'window.scrollTo(0,{height})')
    height += 500
    time.sleep(1)
    if height > max_height:
        break
# b.execute_script('alert("To Bottom")')

import time

from selenium.webdriver import Chrome

from selenium.webdriver.common.keys import Keys

b = Chrome()

b.get('https://mail.163.com/')

# print(b.page_source)

"""
前端在实现网页功能的时候可能出现网页中嵌套网页的现象,如果要在一个网页中嵌套另外一个网页
必须使用iframe标签
selenium爬取的时候,通过浏览器对象默认获取到的是最外层的html对应的网页,如果要获取嵌套
页面中的内容,必须同switch_to来切换frame
"""

iframe = b.find_element_by_css_selector('.loginUrs>iframe')
# iframe1 = b.find_element_by_css_selector('#loginDiv>iframe')

b.switch_to.frame(iframe)

time.sleep(2)

input_account = b.find_element_by_css_selector('.j-inputtext')
input_account.send_keys('123456789')

input_pwd = b.find_element_by_css_selector('.dlpwd')
input_pwd.send_keys('123456789')

login_button = b.find_element_by_css_selector('.u-loginbtn')
login_button.send_keys(Keys.ENTER)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

azured_xu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值