selenium自动化测试微博
时间: 2023-11-13 14:05:55 浏览: 46
以下是使用Selenium进行自动化测试微博的示例代码:
```java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class WeiboTest {
public static void main(String[] args) {
// 设置Chrome浏览器驱动的路径
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
// 创建Chrome浏览器驱动
WebDriver driver = new ChromeDriver();
// 打开新浪微博登录页面
driver.get("https://passport.weibo.cn/signin/login"); // 输入用户名和密码
WebElement username = driver.findElement(By.id("loginName"));
WebElement password = driver.findElement(By.id("loginPassword"));
username.sendKeys("your_username");
password.sendKeys("your_password");
// 点击登录按钮
WebElement loginButton = driver.findElement(By.id("loginAction"));
loginButton.click();
// 等待页面加载完成
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 进入指定微博页面
driver.get("https://m.weibo.cn/status/4588768768768768");
// 获取微博内容
WebElement content = driver.findElement(By.cssSelector("#topic_lists_33 .oriTxt p span"));
System.out.println(content.getText());
// 退出登录
driver.get("https://passport.weibo.cn/logout");
// 关闭浏览器
driver.quit();
}
}
```
这段代码使用Chrome浏览器驱动打开新浪微博登录页面,输入用户名和密码,点击登录按钮,等待页面加载完成后进入指定微博页面,获取微博内容,最后退出登录并关闭浏览器。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)