1.导入相关库
from selenium.webdriver.common.keys import Keys
import time
from selenium import webdriver
2.指定插件位置并启动、初始化chrome
chrome_options = webdriver.ChromeOptions()
extension_path = r'D:\扩展\xpath-helper@latest\xpath-helper.crx'
chrome_options.add_extension(extension_path)
browser = webdriver.Chrome(chrome_options=chrome_options)
3.借用搜狗导航搜索QQ空间
browser.get('https://www.sogou.com/')
browser.find_element_by_id('query').send_keys('QQ空间',Keys.ENTER)
time.sleep(1)
browser.find_element_by_xpath('//*[@id="sogou_vr_30000701_0"]').click()
4.切换窗口
browser.switch_to.window(browser.window_handles[1])
time.sleep(3)
5.定位小窗口 、
browser.switch_to.frame('login_frame')
6.选择账号密码登录
browser.find_element_by_xpath('//*[@id="switcher_plogin"]').click()
time.sleep(1)
7.定位账号、密码输入框并输入账号密码
username = browser.find_element_by_id('u')
passwd = browser.find_element_by_id('p')
username.send_keys('you qq')
passwd.send_keys('you pw')
8.点击确认登陆
browser.find_element_by_xpath('//*[@id="login_button"]').click()
9.完整代码
from selenium.webdriver.common.keys import Keys
import time
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
extension_path = r'D:\扩展\xpath-helper@latest\xpath-helper.crx'
chrome_options.add_extension(extension_path)
browser = webdriver.Chrome(chrome_options=chrome_options)
browser.get('https://www.sogou.com/')
browser.find_element_by_id('query').send_keys('QQ空间',Keys.ENTER)
time.sleep(1)
browser.find_element_by_xpath('//*[@id="sogou_vr_30000701_0"]').click()
browser.switch_to.window(browser.window_handles[1])
time.sleep(3)
browser.switch_to.frame('login_frame')
browser.find_element_by_xpath('//*[@id="switcher_plogin"]').click()
time.sleep(1)
username = browser.find_element_by_id('u')
passwd = browser.find_element_by_id('p')
username.send_keys('')
passwd.send_keys('')
browser.find_element_by_xpath('//*[@id="login_button"]').click()
|