作者:手浪用户2502941303 | 来源:互联网 | 2023-10-11 11:12
1.下载chromedriverhttp:npm.taobao.orgmirrorschromedriver下载与自己GoogleChrome相同版本的chromedriver
1.下载chromedriver
http://npm.taobao.org/mirrors/chromedriver/
下载与自己Google Chrome 相同版本的chromedriver
2.maven导入依赖
org.jsoupjsoup1.10.2
org.seleniumhq.seleniumselenium-java3.141.59
3.完整代码
package com.acofkings;import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;import java.util.concurrent.TimeUnit;public class QQSpaceTest {public static void main(String[] args) throws InterruptedException {String Message="Hello World!";//发表内容String Username="";//qq账号String Passwd="";//qq密码//设置Chrome驱动地址System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\chromedriver.exe");//chromedriver地址//打开Chrome浏览器WebDriver webDriver = new ChromeDriver();webDriver.manage().window().maximize();//删除所有COOKIEswebDriver.manage().deleteAllCOOKIEs();//等浏览器加载完毕,与浏览器同步webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);System.out.println("与浏览器同步成功");webDriver.get("https://qzone.qq.com/");System.out.println("网站名称:"+webDriver.getTitle());//切换到ifram中WebElement iframe =webDriver.findElement(By.id("login_frame"));webDriver.switchTo().frame(iframe);WebElement webElement =webDriver.findElement(By.id("switcher_plogin"));webElement.click();webDriver.findElement(By.id("u")).sendKeys(Username);webDriver.findElement(By.id("p")).sendKeys(Passwd);webDriver.findElement(By.id("login_button")).click();System.out.println("登录成功");Thread.sleep(3000);webDriver.findElement(By.className("music-play")).click();System.out.println("成功静止音乐播放");webDriver.findElement(By.id("menuContainer")).findElement(By.className("menu_item_311")).click();System.out.println("成功切换到说说页面");Thread.sleep(3000);iframe=webDriver.findElement(By.className("app_canvas_frame"));webDriver.switchTo().frame(iframe);System.out.println("成功进入发表说说iframe");webDriver.findElement(By.id("$1_substitutor_content")).click();webDriver.findElement(By.id("$1_content_content")).sendKeys(Message);System.out.println("成功添加内容");webDriver.findElement(By.id("QM_Mood_Poster_Container")).findElement(By.className("op")).click();System.out.println("成功发表说说");Thread.sleep(3000);webDriver.close();System.out.println("网页已关闭");}
}
PS:无法通过验证码,需要电脑上有登录qq即可不需要验证码