import time,os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
target = "https://pan.baidu.com/"
driver.get(target)
name='name'
passwd='passwd'
driver.find_element_by_xpath('//div[@class="account-title"]/a').click()
driver.find_element_by_xpath('//input[@id="TANGRAM__PSP_4__userName"]').send_keys(name)
driver.find_element_by_xpath('//input[@id="TANGRAM__PSP_4__password"]').send_keys(passwd)
driver.find_element_by_xpath('//input[@id="TANGRAM__PSP_4__submit"]').click()
try:
path1 = driver.find_element_by_xpath('//a[@title="test1"]')
ActionChains(driver).move_to_element(path1).perform()
ActionChains(driver).context_click(path1).perform()
driver.find_element_by_xpath('//*[@class="context-menu"]/ul/li[21]').click()
time.sleep(5)
driver.find_element_by_xpath('//*[@class="g-button g-button-blue-large"]/span/span').click()
time.sleep(5)
except:
pass
try:
path2 = driver.find_element_by_xpath('//a[@title="test2"]')
ActionChains(driver).move_to_element(path2).perform()
ActionChains(driver).context_click(path2).perform()
driver.find_element_by_xpath('//*[@class="context-menu"]/ul/li[21]').click()
time.sleep(5)
driver.find_element_by_xpath('//*[@class="g-button g-button-blue-large"]/span/span').click()
time.sleep(5)
except:
pass
上面的代码片段在python3终端中逐行成功运行,
每次在python3终端输入几行,为我做三个任务
1.自动登录pan.baidu.com
2.删除名为test1
的文件
3.删除名为test2
然后将上面的代码段保存为up.py并使用time python3 up.py
运行它。
发生了一件非常有趣的事情 1.运行时间
time python3 up.py
real 0m4.231s
user 0m0.192s
sys 0m0.080s
为什么运行时间少于20秒?
2.第三项任务尚未完成
名为test2
的文件仍然存在,未被程序删除。
如何解释这些奇怪的问题并修复它?
答案 0 :(得分:0)
添加重要的一行:
driver.implicitly_wait(20)