selenium + 石墨文档 自动实现在固定位置写入文字

  • 作为宿舍长,这学期又增加了一个在石墨文档上晚上签到的任务,于是便想敲个代码自动实现这一过程。
    首先,我得承认,这篇文章中的代码有点笨,适应性有点差,大家就把他作为一些函数和坑的文章吧。

  • 前期准备

    环境:window10 + pycharm + python4.7 +selenium
    工具:chrome插件xpath helper
    下载chromedriver.exe
    (https://jingyan.baidu.com/article/f7ff0bfcdd89ed2e27bb1379.html)

  • 代码 + 坑:

	#每次使用perform()移动鼠标的时候,都要移回去 附加解释:(https://blog.csdn.net/Kavin_liu/article/details/51437342)
	
	ActionChains(browser).move_by_offset(100, 200).perform()  
    ActionChains(browser).move_by_offset(-100, -200).perform()  # 将鼠标位置恢复到移动前
""" **有的时候**要做必要的等待:比如说你自己用两次单击组装成一个双击,中间就要做必要的停顿"""
	time.sleep(5)
  	click_locxy(browser,475,290,3)#点击要修改的位置,双击
   	time.sleep(2)
	click_locxy(browser, 116,128, 1)
	time.sleep(2)
  • 完整代码;
    里面涉及到很多私人的东西(账号,密码,石墨文档地址,就这三个),所以,你懂得(不能运行)
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import time

#使用右键或者左键点击页面上的一点
def click_locxy(browser,x,y,method):
    if method==1:#左键单击
        ActionChains(browser).move_by_offset(x, y).click().perform()
    elif method==2:#右键单击
        ActionChains(browser).move_by_offset(x, y).context_click().perform()
    elif method==3:#左键双击
        ActionChains(browser).move_by_offset(x, y).double_click().perform()
    else :
        return
    ActionChains(browser).move_by_offset(-x, -y).perform()  # 将鼠标位置恢复到移动前

if __name__=="__main__":
    #申明浏览器对象
    browser = webdriver.Chrome(executable_path=r'C:\Users\laoge\AppData\Local\Google\Chrome\Application\chromedriver.exe')
    browser.maximize_window()
    browser.get("目标石墨文档")
    click_locxy(browser,768, 382,1)#点击进入登录页面
    #click_locxy(browser,768, 432)#点击进行匿名编辑
    #time.sleep(1)#等待加载

    #输入密码和账号并登录
    try:
        #input_account的xpath和input_passsword的xpath一样
        input_account=browser.find_element_by_name('mobileOrEmail')
        input_password=browser.find_element_by_name("password")
        input_account.send_keys('自己的账号')
        input_password.send_keys('自己的密码')
        click_locxy(browser,748,407,1)#点击登录按钮
    except Exception as e:
        print(str(e))

    #进行编辑操作
    try:
        time.sleep(5)
        click_locxy(browser,475,290,3)#点击要修改的位置,双击
        time.sleep(2)
        click_locxy(browser, 116,128, 1)
        time.sleep(2)
        wait = WebDriverWait(browser, 10)
        input_position=wait.until(EC.presence_of_element_located((By.XPATH,"/html[@class=' ']"
                                                                           "/body/div[@id='root']/div/div[@class='sc-eqIVtm dWJxSL']"
                                                                           "/div[@class='sc-fAjcbJ bsdkjU']/div[@id='sheet-view']"
                                                                           "/div/div[@class='s-body']/div[@class='sm-sheet-editor-wrap sm-sheet-editable']"
                                                                           "/div[@class='sm-sheet-view-container']/div[@class='fx-editor-wrap']"
                                                                           "/div[@id='sm-sheet-fx-editor']")))
        input_position.send_keys(Keys.BACKSPACE)#delete*3键
        input_position.send_keys(Keys.BACKSPACE)
        input_position.send_keys(Keys.BACKSPACE)
        input_position.send_keys('张三丰')
        input_position.send_keys(Keys.ENTER)
        time.sleep(5)#等待页面回传数据
    except Exception as e:
        print("error:"+str(e))
    browser.close()



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值