今天要在公众号发几百篇文章,感觉相当繁琐,点了很长一段时间才发布30多个。这时我就想起了selenium。在网络找了很久,没发现相关源码,实在没办法,只好自己动手。
try{ SeleniumUtil.waitAppearAndClick(driver, 10, By.xpath("//div[@class='new-creation__menu-title']")); Thread.sleep(100); SeleniumUtil.switchToNextTag(driver); Thread.sleep(1000); driver.findElement(By.name("title")).sendKeys(title); driver.findElement(By.name("author")).sendKeys(author); WebDriver frame = driver.switchTo().frame(driver.findElement(By.id("ueditor_0"))); frame.findElement(By.tagName("body")).sendKeys(content); driver.switchTo().defaultContent(); SeleniumUtil.moveToThenClickAnother(driver,By.id("js_cover_area") ,By.xpath("//div[@id='js_cover_null']//a[text()='从图片库选择']")); SeleniumUtil.waitAppearAndClick(driver,10,By.xpath("//div[@id='js_image_dialog_list_wrp']/div[@class='weui-desktop-img-picker__list']/div[@class='weui-desktop-img-picker__item']/i[1]")); SeleniumUtil.click(driver, driver.findElement(By.xpath("//button[text()='下一步']"))); SeleniumUtil.waitAppearAndClick(driver, 10, By.xpath("//button[text()='完成']")); Thread.sleep(3000); driver.findElement(By.id("js_description")).sendKeys(des); SeleniumUtil.waitAppear(driver, 10, By.xpath("//div[@id='js_original']//input[@type='checkbox']")); SeleniumUtil.click(driver, driver.findElement(By.xpath("//div[@id='js_original']//input[@type='checkbox']"))); SeleniumUtil.waitAppear(driver, 10, By.id("js_copyright_agree")); SeleniumUtil.makeSureCheckbox(driver, By.xpath("//input[@id='js_copyright_agree']"), 30); try{ SeleniumUtil.waitAppear(driver, 3, By.xpath("//button[text()='下一步']")); SeleniumUtil.click(driver, driver.findElement(By.xpath("//button[text()='下一步']"))); Thread.sleep(20); }catch(Exception ee){ } SeleniumUtil.waitAppearAndClick(driver, 10,By.xpath("//div[@id='js_original_edit_box']//label[text()='文章类别']/../div[@class='weui-desktop-form__controls']/div[1]")); Thread.sleep(20); SeleniumUtil.click(driver, driver.findElement(By.xpath("//span[text()='星星物语']"))); Thread.sleep(20); SeleniumUtil.click(driver, driver.findElement(By.xpath("//span[text()='牛郎']"))); Thread.sleep(20); SeleniumUtil.click(driver, driver.findElement(By.xpath("//button[text()='确定']"))); Thread.sleep(20); SeleniumUtil.moveToThenClickAnother(driver,By.xpath("//div[@class='js_appmsg_publish_wrp']/li[@class='icon_toggle_edit']/div[@class='border_of_js_publish']") ,By.id("js_publish_freely")); SeleniumUtil.waitAppear(driver, 10, By.xpath("//div[@class='weui-desktop-dialog']//button[text()='发布']")); SeleniumUtil.click(driver, driver.findElement(By.xpath("//button[text()='发布']"))); SeleniumUtil.waitUntilUrlContain(driver, 100, "https://mp.weixin.qq.com/cgi-bin/home"); SeleniumUtil.closeTag(driver); //发布成功的要删掉 System.out.println(title); FileUtil.del(path); }catch(Exception e){ e.printStackTrace(); }