a=driver.find_element(By.XPATH, '//*[@id="username"]')#send_keys("18682883667") # 输入账号 a.clear() a.send_keys("18682883667")
时间: 2024-05-17 07:12:39 浏览: 10
这行代码使用 Selenium 的 `find_element` 方法根据 XPATH 定位页面中的用户名输入框,并将其赋值给变量 `a`。接下来,使用 `a.clear()` 方法清空输入框内的内容,并使用 `a.send_keys("18682883667")` 方法输入新的用户名 "18682883667"。这样就可以模拟用户在页面中输入账号。
相关问题
from selenium import webdriver from selenium.webdriver.common.keys import Keys import time # 创建Chrome浏览器驱动对象 driver = webdriver.Chrome() driver.maximize_window() # 打开网页 driver.get("https://www.job5156.com/") time.sleep(10) # 定位登录按钮并点击 login_button = driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div[1]/div[1]/div/span") login_button.click() time.sleep(3) # 定位密码登录按钮并点击 password_login_button = driver.find_element_by_xpath("""//*[@id="commonLoginBox"]/div[2]/form/div[1]/div[2]""") password_login_button.click() # 定位用户名输入框并输入用户名 username_input = driver.find_element_by_xpath("""//*[@id="login_box_account"]""") username_input.send_keys("your_username") # 定位密码输入框并输入密码 password_input = driver.find_element_by_xpath("""//*[@id="login_box_password"]""") password_input.send_keys("your_password") # 定位登录按钮并点击 submit_button = driver.find_element_by_xpath("""//*[@id="commonLoginBox"]/div[2]/form/p[2]""") submit_button.click() #等待页面加载完成 driver.implicitly_wait(10) # 关闭浏览器 driver.quit()这代码不成功哪里有错误
这段代码有一个问题:登录按钮的定位方式可能不是最佳的方式。如果该网站的页面元素更改,可能会导致无法找到该按钮,从而导致无法模拟登录。
我建议您使用更可靠的元素定位方式,例如使用 `id` 属性来定位元素。另外,您还需要检查您的用户名和密码是否正确,并且等待页面加载完全后再进行操作。
以下是修改后的示例代码:
```python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
# 创建 Chrome 浏览器驱动对象
driver = webdriver.Chrome()
driver.maximize_window()
# 打开网页
driver.get("https://www.job5156.com/")
time.sleep(10)
# 定位登录按钮并点击
login_button = driver.find_element_by_xpath('//*[@id="login_box"]/div[1]/div/div/ul/li[1]/a')
login_button.click()
# 定位密码登录按钮并点击
password_login_button = driver.find_element_by_xpath('//*[@id="login_box"]/div[2]/div[2]/div[1]/a')
password_login_button.click()
# 定位用户名输入框并输入用户名
username_input = driver.find_element_by_xpath('//*[@id="userName"]')
username_input.send_keys("your_username")
# 定位密码输入框并输入密码
password_input = driver.find_element_by_xpath('//*[@id="password"]')
password_input.send_keys("your_password")
# 定位登录按钮并点击
submit_button = driver.find_element_by_xpath('//*[@id="loginBtn"]')
submit_button.click()
# 等待页面加载完成
driver.implicitly_wait(10)
# 关闭浏览器
driver.quit()
```
希望这个指导对您有帮助!
改进这段代码import time from time import sleep from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.ui import Select import csv import unittest import ddt driver = webdriver.Chrome() driver.maximize_window() driver.get("http://10.2.39.8/ams/front/login.do?gotourl=http%3A%2F%2F10.2.39.8%2Fams%2Ffront%2Fasset%2Fasset_list.do") driver.implicitly_wait(5) driver.find_element_by_xpath('//*[@id="fmedit"]/div[1]/label[2]/input').click() driver.find_element_by_xpath('//*[@id="taskId"]').send_keys('23') driver.find_element_by_xpath('//*[@id="loginName"]').send_keys('20210205') driver.find_element_by_xpath('//*[@id="password"]').send_keys('20210205') driver.find_element_by_xpath('//*[@id="fmedit"]/div[7]/button').click() driver.find_element_by_xpath('//*[@id="leftmenu_asset_brand"]/a').click() driver.find_element_by_xpath('//*[@id="content"]/div[2]/div/div[1]/button').click() driver.find_element_by_id("title").send_keys("username") driver.find_element_by_id("code").send_keys("password") driver.find_element_by_css_selector('#submitButton').click()使用csv文件参数化username和password
可以使用ddt库来实现csv文件参数化,代码如下:
```
import time
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import Select
import csv
import unittest
import ddt
@ddt.ddt
class TestLogin(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.driver.get("http://10.2.39.8/ams/front/login.do?gotourl=http%3A%2F%2F10.2.39.8%2Fams%2Ffront%2Fasset%2Fasset_list.do")
self.driver.implicitly_wait(5)
def tearDown(self):
self.driver.quit()
@ddt.file_data('test_data.csv')
def test_login(self, username, password):
self.driver.find_element_by_xpath('//*[@id="fmedit"]/div[1]/label[2]/input').click()
self.driver.find_element_by_id("taskId").send_keys('23')
self.driver.find_element_by_id("loginName").send_keys(username)
self.driver.find_element_by_id("password").send_keys(password)
self.driver.find_element_by_css_selector('#fmedit > div:nth-child(7) > button').click()
self.assertIn('登录成功', self.driver.page_source)
```
其中,test_data.csv文件的格式为:
```
username,password
20210205,20210205
20210305,20210305
20210405,20210405
```
这样,就可以通过ddt库和csv文件实现对用户名和密码的参数化,从而实现自动化测试。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![jar](https://img-home.csdnimg.cn/images/20210720083455.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)