def fenpei():#循环分配 driver.find_element_by_xpath("/html/body/div/div/div[2]/section/div/div[1]/div/div/ul/li[2]/span").click() sleep(1) print("点击待分配") test_danshu=driver.find_element_by_xpath("//*[@id='app']/div/div[2]/section/div/div[2]/div[2]/div/div[1]").text if test_danshu=="全部: 0单 禁:0单 预计批款总额: 0万": print("---------一单都没---------") driver.close() else: sleep(1) driver.find_element_by_xpath("//*[@id='app']/div/div[2]/section/div/div[2]/div[2]/div/div[2]/div/div[2]/div[8]/div/button[1]/span").click() print("点击派单") i = 0 while True: i += 1 print(i) test_paid = driver.find_element_by_xpath(f"//*[@id='app']/div/div[2]/section/div/div[2]/div[3]/table/tbody/tr[1]/td[{i}]/div/span").text print("12") if test_paid != "可预约": print("不能预约") if driver.find_element_by_xpath("//*[@id='app']/div/div[2]/section/div/div[2]/div[3]/table/tbody/tr[1]/td[15]/div/span"): print("第15个") element = driver.find_element_by_xpath("//*[@id='app']/div/div[2]/section/div/div[2]/div[3]") print("定位滚动条") sleep(1) driver.execute_script("arguments[0].scrollLeft = window.innerWidth", element) print("滑动") sleep(1) else: print("没有找到第15个") else: test_paid.click() print("122")为什么不执行print("12")
时间: 2024-04-27 09:19:37 浏览: 160
从代码中来看,如果 `test_paid` 不等于 "可预约",则会判断是否存在第 15 个元素,如果存在则滑动滚动条,否则打印 "没有找到第15个"。在这个过程中,如果 `test_paid` 不等于 "可预约",则不会执行 `test_paid.click()` 以及后面的代码,所以也不会执行 `print("12")`。可能的原因是:
- `test_paid` 的值一直为 "不能预约",导致没有执行 `test_paid.click()`。
- 在查找第 15 个元素时出现了异常,导致没有执行滑动滚动条的代码。
建议在代码中添加一些调试信息,例如打印 `test_paid` 的值,或者在滑动滚动条的代码前添加一个 `try-except` 语句来捕获异常,并打印异常信息,以便找到出错的原因。
相关问题
from selenium import webdriver import time # 导入selenium4 from selenium.webdriver.common.by import By # 创建一个浏览器实例 driver = webdriver.Chrome() # 打开网站 driver.get('http://192.168.31.114:4399/') time.sleep(3) # 输入账号 driver.find_element(By.XPATH, "/html/body/div[1]/div/div/div[1]/div[2]/div[1]/div/v-card-texts/form/div[1]/div[1]/div/div[3]/input").send_keys("13732237096") # 输入密码 driver.find_element(By.XPATH, "/html/body/div[1]/div/div/div[1]/div[2]/div[1]/div/v-card-texts/form/div[2]/div[1]/div/div[3]/input").send_keys( "zxcvbnm123") # 点击空白处等待登陆按钮生效 driver.find_element(By.CLASS_NAME, "v-messages").click() # 点击登陆 driver.find_element(By.XPATH, "/html/body/div[1]/div/div/div[1]/div[2]/div[1]/div/v-card-texts/form/button").click() time.sleep(2) # 关闭浏览器 driver.quit() print("注册成功")这段代码需要怎么优化
这段代码可以进行如下优化:
1. 使用显式等待代替 time.sleep(),可以更加准确地等待页面元素加载完成。
2. 使用 Page Object 设计模式,将页面元素和操作封装到一个 Page 类中,提高代码的可维护性。
3. 使用配置文件或命令行参数来传递账号和密码,避免将敏感信息硬编码在代码中。
下面是一个可能的优化示例:
```
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class LoginPage:
url = 'http://192.168.31.114:4399/'
account_input = (By.XPATH, "/html/body/div[1]/div/div/div[1]/div[2]/div[1]/div/v-card-texts/form/div[1]/div[1]/div/div[3]/input")
password_input = (By.XPATH, "/html/body/div[1]/div/div/div[1]/div[2]/div[1]/div/v-card-texts/form/div[2]/div[1]/div/div[3]/input")
login_button = (By.XPATH, "/html/body/div[1]/div/div/div[1]/div[2]/div[1]/div/v-card-texts/form/button")
def __init__(self, driver):
self.driver = driver
def open(self):
self.driver.get(LoginPage.url)
def input_account(self, account):
element = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(LoginPage.account_input))
element.send_keys(account)
def input_password(self, password):
element = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(LoginPage.password_input))
element.send_keys(password)
def click_login_button(self):
element = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(LoginPage.login_button))
element.click()
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--account', required=True, help='Login account')
parser.add_argument('--password', required=True, help='Login password')
args = parser.parse_args()
driver = webdriver.Chrome()
login_page = LoginPage(driver)
login_page.open()
login_page.input_account(args.account)
login_page.input_password(args.password)
login_page.click_login_button()
driver.quit()
print("登陆成功")
```
在这个示例中,我们使用了 Page Object 设计模式,将页面元素和操作都封装到了 LoginPage 类中。在主程序中,我们使用了 argparse 模块来解析命令行参数,避免了将账号和密码硬编码在代码中。同时,我们使用了显式等待来等待页面元素加载完成。
def test_10(self): self.driver.get("http://oa.hhero.com.cn/?m=login")#访问网址 self.driver.set_window_size(1051, 798)#设置窗口大小 self.driver.find_element(By.NAME, "adminuser").click()#点击用户名输入框 self.driver.find_element(By.NAME, "adminuser").send_keys("admin")#输入用户名 self.driver.find_element(By.CSS_SELECTOR, "div:nth-child(3) .input").click()#点击密码输入框 self.driver.find_element(By.CSS_SELECTOR, "div:nth-child(3) .input").send_keys("a123456")#输入密码 self.driver.find_element(By.NAME, "button").click()#点击登录按钮 self.driver.implicitly_wait(5)#隐式等待5s self.driver.find_element(By.ID, "indesearchmenu").click()#点击搜索 self.driver.find_element(By.ID, "confirm_input").click()#点击搜索跳出的输入框 self.driver.find_element(By.ID, "confirm_input").send_keys("流程")#输入关键词"流程" self.driver.find_element(By.ID, "confirm_btn1").click()#点击确定 self.driver.find_element(By.LINK_TEXT, "流程模块列表").click()#点击"流程模块列表" self.driver.find_element(By.XPATH, "/html/body/table/tbody/tr/td[3]/div/div[4]/div/div[2]/div[1]/table/tbody/tr/td[2]/div/input").click()#在流程模块列表界面点击输入框 self.driver.find_element(By.XPATH, "/html/body/table/tbody/tr/td[3]/div/div[4]/div/div[2]/div[1]/table/tbody/tr/td[2]/div/input").send_keys("图书管理")#在输入框中输入图书管理 self.driver.find_element(By.CSS_SELECTOR, ".input-group-btn > .btn").click()#点击搜索 self.vars["types"] = self.driver.find_element(By.XPATH, "/html/body/table/tbody/tr/td[3]/div/div[4]/div/div[2]/div[3]/div[1]/table/tbody/tr/td[2]").text#获取其类型 print("{}".format(self.vars["types"])) 这段代码的测试用例都有哪些
这段代码包含了一个名为 `test_10` 的测试用例。该测试用例包括以下步骤:
1. 使用 `get()` 方法访问网址 "http://oa.hhero.com.cn/?m=login"。
2. 使用 `set_window_size()` 方法设置浏览器窗口大小为 (1051, 798)。
3. 使用 `find_element()` 方法和 `click()` 方法分别点击用户名输入框和密码输入框。
4. 使用 `find_element()` 方法和 `send_keys()` 方法分别输入用户名 "admin" 和密码 "a123456"。
5. 使用 `find_element()` 方法和 `click()` 方法点击登录按钮。
6. 使用 `implicitly_wait()` 方法隐式等待 5 秒。
7. 使用 `find_element()` 方法和 `click()` 方法分别点击搜索和搜索跳出的输入框。
8. 使用 `find_element()` 方法和 `send_keys()` 方法输入关键词 "流程"。
9. 使用 `find_element()` 方法和 `click()` 方法点击确定按钮。
10. 使用 `find_element()` 方法和 `click()` 方法点击 "流程模块列表"。
11. 使用 `find_element()` 方法和 `click()` 方法在流程模块列表界面点击输入框。
12. 使用 `find_element()` 方法和 `send_keys()` 方法在输入框中输入 "图书管理"。
13. 使用 `find_element()` 方法和 `click()` 方法点击搜索按钮。
14. 使用 `find_element()` 方法获取元素的文本,并将其赋值给字典变量 `self.vars["types"]`。
15. 使用 `print()` 函数打印出字典变量 `self.vars["types"]` 的值。
这个测试用例的目的是在指定的网页中进行一系列的操作,并获取特定元素的文本值进行验证。这些步骤可以用于测试网页的搜索功能,并验证搜索结果中的类型。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.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://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)