一-芝麻代理作业
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
s = Service(executable_path='../day32/chromedriver.exe')
browser = webdriver.Chrome(service=s)
URL = 'https://jahttp.zhimaruanjian.com/'
browser.get(url=URL)
# 作业内容:显式等待芝麻代理网站用户名是否出现
flag = EC.text_to_be_present_in_element(
(By.XPATH, '/html/body/section/aside/div[1]/a'),
'zmhttp994457'
)
WebDriverWait(browser, timeout=60).until(flag)
print('登陆成功')
二-webdriver-manager模块的使用
1、webdriver-manager介绍
1.谷歌浏览器版本需要和selenium所需驱动版本一一对应,webdriver-manager可以自动检测浏览器的版本自动下载驱动。
2.webdriver-manager运行只需几行代码,并且官方文档给了,可以直接复制。
2、安装
pip install webdriver-manager
查看已安装三方模块细节:pip show webdriver-manager
3、使用
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
# 从webdriver-manager的chrome包中导入ChromeDriverManager方法
from webdriver_manager.chrome import ChromeDriverManager
# ChromeDriverManager().install():自动检测电脑上的Chrome浏览器的版本,下载对应驱动
driver_path = './'
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager(path=driver_path).install()))
三-iframe标签切换
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
# 从webdriver-manager的chrome包中导入ChromeDriverManager方法
from webdriver_manager.chrome import ChromeDriverManager
# ChromeDriverManager().install():自动检测电脑上的Chrome浏览器的版本,下载对应驱动
driver_path = './'
browser = webdriver.Chrome(service=ChromeService(ChromeDriverManager(path=driver_path).install()))
browser.implicitly_wait(10)
URL = 'https://y.qq.com/'
browser.get(url=URL)
# 点击QQ音乐右上角登录按钮
browser.find_element(By.XPATH, '//*[@id="app"]/div/div[1]/div/div[2]/span/a').click()
# 先进行iframe标签切换:iframe标签从外向内一层一层切换
iframe_1 = browser.find_element(By.XPATH, '//*[@id="login_frame"]')
browser.switch_to.frame(iframe_1)
iframe_2 = browser.find_element(By.XPATH, '//*[@id="ptlogin_iframe"]')
browser.switch_to.frame(iframe_2)
# 此时,browser就从最外层页面到了最内层iframe,如果还想再切回去
# browser.switch_to.default_content():可以一步到位直接切到最外层
# 再从弹出来的弹窗中点击”密码登录“
browser.find_element(By.XPATH, '//*[@id="switcher_plogin"]').click()
# 输入账号密码、点击登录
input_field_username = browser.find_element(By.XPATH, '//*[@id="u"]')
input_field_username.send_keys('你的账号')
input_field_password = browser.find_element(By.XPATH, '//*[@id="p"]')
input_field_password.send_keys('你的密码')
browser.find_element(By.XPATH, '//*[@id="login_button"]').click()
四-淘宝页面爬取之输入账号密码
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
# 从webdriver-manager的chrome包中导入ChromeDriverManager方法
from webdriver_manager.chrome import ChromeDriverManager
# ChromeDriverManager().install():自动检测电脑上的Chrome浏览器的版本,下载对应驱动
driver_path = './'
# 引入配置项
s = ChromeService(ChromeDriverManager(path=driver_path).install())
Options = webdriver.ChromeOptions()
# 引入不关闭浏览器的相关配置项
Options.add_experimental_option("detach", True)
# 避免终端下执行代码报警告
Options.add_experimental_option("excludeSwitches", ['enable-automation', 'enable-logging'])
browser = webdriver.Chrome(service=s, options=Options)
URL = 'https://www.taobao.com/'
browser.get(url=URL)
# 淘宝必须登录,否则没办法使用
# 点击淘宝页面上方的”亲,请登录“
browser.find_element(By.XPATH, '//*[@id="J_SiteNavLogin"]/div[1]/div[1]/a[1]').click()
# 寻找账号密码输入框,点击登录按钮
user_name = input('请输入账号:')
browser.find_element(By.XPATH, '//*[@id="fm-login-id"]').send_keys(user_name)
import getpass
# getpass模块中的getpass方法,能够不显示输入的内容
# 如果程序用了getpass方法,不能直接右键执行,必须在终端中使用命令执行:python ./day34/04-淘宝页面爬取(一).py
password = getpass.getpass('请输入密码:')
browser.find_element(By.XPATH, '//*[@id="fm-login-password"]').send_keys(password)