@Time : 2023/7/12 15:39
@File : 在线文档写入数据.py
实现在线文档的编辑三步走:
1、设置浏览器和代理调用UI自动化操作的时候直接操作已打开的浏览器
2、登录在线文档:在打开的浏览器打开并登录在线文档--此文直接进入需要操作的文档
3、通过键盘操作来实现文档内容的增删改查【登录用户要具备相应的权限】
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
driver = webdriver.Chrome(options=chrome_options)
driver.get('https://docs.qq.com/sheet/DTEtLSW5NWldEcWJE?tab=gqke19')
print(driver.title)
j = 0
driver.find_element(By.XPATH, '//*[@id="alloy-simple-text-editor"]').send_keys(Keys.HOME)
driver.find_element(By.XPATH, '//*[@id="alloy-simple-text-editor"]').send_keys(Keys.CONTROL, Keys.UP)
driver.find_element(By.XPATH, '//*[@id="alloy-simple-text-editor"]').send_keys(Keys.ENTER)
driver.find_element(By.XPATH, '//*[@id="alloy-simple-text-editor"]').send_keys(Keys.TAB)
list = [1,2,3,54]
for i in range(300):
driver.find_element(By.XPATH, '//*[@id="alloy-simple-text-editor"]').send_keys(Keys.HOME)
driver.find_element(By.XPATH, '//*[@id="alloy-simple-text-editor"]').click()
s = driver.find_element(By.XPATH,'//*[@class="bar-label"]').text
a = int(s[1:])-1
print(a)
driver.find_element(By.XPATH,'//*[@id="alloy-simple-text-editor"]').click()
txt = driver.find_element(By.XPATH,'//*[@id="alloy-simple-text-editor"]/p').text
if txt =='':
for i in range(len(list)):
driver.find_element(By.XPATH, '//*[@id="alloy-simple-text-editor"]/p').send_keys(list[i])
driver.find_element(By.XPATH,'//*[@id="alloy-simple-text-editor"]').send_keys(Keys.TAB)
driver.find_element(By.XPATH, '//*[@id="alloy-simple-text-editor"]').send_keys(Keys.ENTER)
try:
driver.find_element(By.XPATH, '//*[text()="添加"]').click()
except:
print('不需要扩展列表')
python2.0+的在线编辑器:
http://www.runoob.com/try/runcode.php?filename=HelloWorld&type=python
python3.0的在线编辑器:
http://www.runoob.com/try/runcode.p...
# 定位表格输入框并填写内容
input_box = driver.find_element_by_xpath("//input[@class='input__element']")
input_box.send_keys("这是一个测试")
# 提交表格
submit_button = driver.find_element_by_xpath("//button[@class='button__submit']")
submit_button.click()
# 关闭浏览器
driver.quit()
你需要根据实际情况修改代码中的表格链接、输入框和按钮位置等信息。同时,你需要下载对应版本的 ChromeDriver,并将其路径添加到系统环境变量中。