nodejs selenium 发布 osc 博客[需要cookie]

穿精又带淫゛_ 提交于 2020-07-28 20:50:02

可以发布成功, 但是效果不好

 

首先无法获取到iframe, 这样也就无法直接赋值, 只能一个一个输入, 在输入的时候遇到闭合标签的情况, 会自动闭合, 造成代码乱了代码最后会多出来闭合的标签, 其实可以使用del删除掉, 但是这样很麻烦而且selenium 本身就很慢

 

对于换行的问题, markdown使用tab后换行会默认对齐, 需要使用shift+enter另起一行

 

 

代码

const firefox = require('selenium-webdriver/firefox');
const chrome = require('selenium-webdriver/chrome');
const path = 't.md'
const fs = require('fs')
const {Builder, By, Key} = require('selenium-webdriver');
const CONTENT = fs.readFileSync(path, 'utf8').split('\n')

const cookie = "==="


const os = require('os');
let platform = os.platform();
console.log(platform)

const WAIT_TIME = 200
const url = 'https://my.oschina.net/ahaoboy'

function waitTime(n) {
  return new Promise(resolve => setTimeout(
    () => resolve()
    , n
  ))
}

async function play(url) {
  const chromeType = platform === 'win32' ? 'chrome' : 'firefox'
  let options = chromeType === 'chrome' ? new chrome.Options() : new firefox.Options()
  options.headless()
  let driver = await new Builder().forBrowser(chromeType)
    // .setFirefoxOptions(options)
    .setChromeOptions(options)
    .build(options);
  let op = driver.manage()
  for (let i of cookie.split(';')) {
    let [name, value] = i.split('=')
    name = name.trim()
    value = value.trim()
    op.addCookie(
      {
        name,
        value
      }
    )
  }
  try {
    await driver.get(url);
    // 刷新后就可以根据cookie自动登录了
    await driver.navigate().refresh()

    // 进入写文章页面
    let btn = await driver.findElement(By.className('write-btn'))
    await btn.click()
    await waitTime(WAIT_TIME)


    // 选择markdown模式
    let link = await driver.findElement(By.linkText('Markdown'))
    await link.click()
    await waitTime(WAIT_TIME * 5)

    const actions = driver.actions();
    for (let line of CONTENT) {
      await actions.sendKeys(...line)
      await actions.keyDown(Key.SHIFT)
        .keyDown(Key.ENTER)
        .keyUp(Key.ENTER)
        .keyUp(Key.SHIFT)
    }
    await actions.perform();

    // 选择分类
    let type = await driver.findElement(By.className('ui dropdown selection'))
    await type.click()
    await waitTime(WAIT_TIME)
    let divs = await driver.findElement(By.className('menu transition visible'))
    let itemList = await divs.findElements(By.className('item'))
    await waitTime(WAIT_TIME)
    await itemList[2].click()

    // 标题
    let titleBox = await driver.findElement(By.className('two fields'))
    let title = await titleBox.findElement(By.name('title'))
    title.sendKeys('webdriver', Key.RETURN);
    await waitTime(WAIT_TIME * 2)

    // 选择分类
    let downBox = await driver.findElement(By.className('required field set-bottom'))
    let downDiv = await downBox.findElement(By.className('ui dropdown selection'));
    await downDiv.click()
    await waitTime(WAIT_TIME)
    let list = await downBox.findElements(By.className('item'))
    list[5].click()

    // 点击发布
    let subBtn = await driver.findElement(By.className('ui primary large submit button'))
    subBtn.click()
    await waitTime(WAIT_TIME * 5)
  } catch (e) {
    console.log('==>', e)
    return -1
  } finally {
    await driver.quit();
  }
}


play(url)

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!