获取输入框信息browser.find_element_by_xpath("//*[@id="app"]/div/div[2]/section/div/div[1]/div/div[2]/div[2]/div/div[2]/div[2]/div[1]/div[2]/div[1]/div[1]/form/div[5]/div/div/div/div/input").
时间: 2023-09-27 17:11:58 浏览: 118
你可以使用 Selenium 的 get_attribute 方法获取输入框中的信息。示例代码如下:
```python
input_box = browser.find_element_by_xpath("//*[@id='app']/div/div[2]/section/div/div[1]/div/div[2]/div[2]/div/div[2]/div[2]/div[1]/div[2]/div[1]/div[1]/form/div[5]/div/div/div/div/input")
input_text = input_box.get_attribute("value")
```
其中,get_attribute 方法的参数是要获取的属性名称。因为输入框中的信息实际上是作为 value 属性的值存储的,所以我们使用 value 作为参数获取输入框中的信息。获取到的信息保存在 input_text 变量中。你可以根据实际情况替换 xpath 以及变量名。
相关问题
改进这段代码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文件实现对用户名和密码的参数化,从而实现自动化测试。
优化这段代码driver = webdriver.Chrome() driver.get("http://cjwxb-test.dianpusoft.cn/userLogin") driver.get("http://cjwxb-test.dianpusoft.cn/new-interact/interact/setActivityTemplate?activityType=10021") time.sleep(3) driver.find_element(By.XPATH, '//*[@id="setActivityTemplate"]/div/div[2]/form/div[3]/div[1]/div').click() time.sleep(2) driver.find_element(By.XPATH, '//*[@id="setActivityTemplate"]/div/div[2]/form/div[3]/div[1]/div/div/button').click() time.sleep(2) driver.find_element(By.XPATH, '//*[@id="editTemplatePage"]/div/div[3]/div[2]/button[2]').click() driver.find_element(By.XPATH, '//*[@id="activitySetting1 dataForm"]/div/div[3]/form/div[1]/div/div/div[2]/div/input').clear() time.sleep(2) driver.find_element(By.XPATH, '//*[@id="activitySetting1 dataForm"]/div/div[3]/form/div[1]/div/div/div[2]/div/input').send_keys("AT") driver.find_element(By.XPATH, '//*[@id="activitySetting1 dataForm"]/div/div[3]/form/div[3]/div/div/div[2]/label[2]/span[1]/span').click() driver.find_element(By.XPATH, '//*[@id="activitySetting1 dataForm"]/div/div[3]/form/div[3]/div/div/div[3]/div[2]/label/span[1]/span').click() win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, 0, 0, -1000) driver.find_element(By.XPATH, '//*[@id="activitySetting1 dataForm"]/div/div[4]/div/form/div[1]/div[2]/div/div[1]/div/span[2]').click() driver.find_element(By.XPATH, '//*[@id="activitySetting1 dataForm"]/div/div[4]/div/form/div[2]/div/button/span').click()
可以尝试以下优化方式:
1. 使用显式等待代替 time.sleep(),可以更加精准地等待页面元素的加载,提高代码的执行效率。
2. 将重复的元素定位代码提取出来,封装成函数,减少代码冗余。
3. 将多个操作封装成一个函数,提高代码的可读性和可维护性。
下面是优化后的代码:
```python
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import win32api
import win32con
def wait_for_element(driver, locator):
return WebDriverWait(driver, 10).until(EC.presence_of_element_located(locator))
def click_element(driver, locator):
element = wait_for_element(driver, locator)
element.click()
def set_text(driver, locator, text):
element = wait_for_element(driver, locator)
element.clear()
element.send_keys(text)
def scroll_down(driver):
win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, 0, 0, -1000)
def set_activity_template(driver):
driver.get("http://cjwxb-test.dianpusoft.cn/new-interact/interact/setActivityTemplate?activityType=10021")
click_element(driver, (By.XPATH, '//*[@id="setActivityTemplate"]/div/div[2]/form/div[3]/div[1]/div'))
click_element(driver, (By.XPATH, '//*[@id="setActivityTemplate"]/div/div[2]/form/div[3]/div[1]/div/div/button'))
click_element(driver, (By.XPATH, '//*[@id="editTemplatePage"]/div/div[3]/div[2]/button[2]'))
set_text(driver, (By.XPATH, '//*[@id="activitySetting1 dataForm"]/div/div[3]/form/div[1]/div/div/div[2]/div/input'), "AT")
click_element(driver, (By.XPATH, '//*[@id="activitySetting1 dataForm"]/div/div[3]/form/div[3]/div/div/div[2]/label[2]/span[1]/span'))
click_element(driver, (By.XPATH, '//*[@id="activitySetting1 dataForm"]/div/div[3]/form/div[3]/div/div/div[3]/div[2]/label/span[1]/span'))
scroll_down(driver)
click_element(driver, (By.XPATH, '//*[@id="activitySetting1 dataForm"]/div/div[4]/div/form/div[1]/div[2]/div/div[1]/div/span[2]'))
click_element(driver, (By.XPATH, '//*[@id="activitySetting1 dataForm"]/div/div[4]/div/form/div[2]/div/button/span'))
driver = webdriver.Chrome()
driver.get("http://cjwxb-test.dianpusoft.cn/userLogin")
set_activity_template(driver)
```
阅读全文