Selenium WebDriver 测试登录
时间: 2023-11-21 10:41:48 浏览: 44
好的,下面是一个基本的 Selenium WebDriver 测试登录的示例代码,假设你要测试的网站是 https://example.com:
```python
from selenium import webdriver
# 创建 WebDriver 实例
driver = webdriver.Chrome()
# 打开登录页面
driver.get("https://example.com/login")
# 输入用户名和密码并点击登录按钮
username = driver.find_element_by_name("username")
password = driver.find_element_by_name("password")
username.send_keys("your_username")
password.send_keys("your_password")
login_button = driver.find_element_by_xpath("//button[contains(text(), 'Login')]")
login_button.click()
# 验证登录成功
welcome_message = driver.find_element_by_xpath("//h1[contains(text(), 'Welcome')]")
assert welcome_message.is_displayed()
# 关闭浏览器
driver.quit()
```
以上代码使用 Chrome 浏览器作为 WebDriver,打开登录页面,输入用户名和密码,点击登录按钮,然后验证登录成功。请替换 `your_username` 和 `your_password` 为实际的用户名和密码。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)