作者:妩媚天天想我 | 来源:互联网 | 2023-05-18 07:06
Selenium很好使用,下面写了三个函数,分别用来:login_163()–>登陆163邮箱(手动点击验证码)login_qzone()–>登陆QQ空间(快捷登
Selenium很好使用,下面写了三个函数,分别用来:
login_163() –> 登陆163邮箱(手动点击验证码)
login_qzone() –> 登陆QQ空间(快捷登录)
login_sina_post() –> 登陆新浪邮箱并发送邮件
其中发送邮件部分,忙活了大半天,各种百度,总算解决。遇到不少经典问题,在后面列出。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import ElementNotVisibleException
import time
def login_163():
driver = webdriver.Chrome()
driver.get('http://mail.163.com/')
time.sleep(3)
driver.switch_to_frame('x-URS-iframe')
time.sleep(3)
elem_user = driver.find_element_by_name('email')
elem_user.clear()
elem_user.send_keys('账号')
elem_pwd = driver.find_element_by_name('password')
elem_pwd.clear()
elem_pwd.send_keys('密码')
elem_pwd.send_keys(Keys.RETURN)
time.sleep(10)
driver.find_element_by_id("dologin").click()
def login_qzone():
driver = webdriver.Chrome()
driver.get('http://i.qq.com/')
driver.switch_to_frame('login_frame')
time.sleep(3)
driver.find_element_by_id('img_out_QQ号').click()
def login_sina_post():
driver = webdriver.Chrome()
print('打开网页')
driver.set_window_size(1120, 550)
driver.get('http://mail.sina.com.cn/?from=mail#')
time.sleep(2)
print('输入账号及密码')
elem_user = driver.find_element_by_id('freename')
elem_user.clear()
elem_user.send_keys('账号')
elem_pwd = driver.find_element_by_id('freepassword')
elem_pwd.clear()
elem_pwd.send_keys('密码')
driver.find_element_by_class_name('loginBtn').click()
time.sleep(3)
driver.find_element_by_css_selector("a[_act='navigate'][_appid='writer']").click()
time.sleep(3)
print('输入收件人')
elem_address=driver.find_element_by_class_name('wui-AddrInput')
action_a = ActionChains(driver)
action_a.move_to_element(elem_address).click().send_keys('收件人邮箱').send_keys(Keys.RETURN).perform()
print('输入主题')
driver.find_element_by_name('subj').send_keys('Test')
print('输入邮件内容')
elem_frame = driver.find_element_by_class_name('iframe')
driver.switch_to_frame(elem_frame)
driver.find_element_by_xpath('//body').send_keys('this is just a test based on selenium')
print('点击发送')
driver.switch_to.default_content()
driver.find_element_by_class_name('mailPubText').click()
time.sleep(1)
print('发送成功')
driver.quit()
login_sina_post()
相关问题:
① 设置等待时间,等待页面加载之后再操作
②报错
1)selenium.common.exceptions.WebDriverException: Message: unknown error: cannot focus element
解决:
elem_address=driver.find_element_by_class_name('wui-AddrInput')
action_a = ActionChains(driver)
action_a.move_to_element(elem_address).click().send_keys('18846080950@163.com').send_keys(Keys.RETURN).perform()
详情参考(中间详细讲了几种可能)
2)Message: invalid selector: Compound class names not permitted
解决: 有两个类名不能用空格,前后都要用 .
3)selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
存在frame,要切换到该frame中才能找到
最后要记得跳出frame
可以参考Python+Selenium定位不到元素常见原因及解决办法(报:NoSuchElementException)
driver.switch_to_frame('login_frame')
很多时候是因为,搜索的不唯一,导致搜到别的地方,结果出错
③对于运行报错
一定先到stackoverflow搜索,直接搜报错原因,多看评论,多尝试