WebElement.clear() 方法的具体实现

本文详细介绍了如何使用WebDriver找到具有特定ID的输入元素,并将其设置为指定的文本值。通过调用sendKeys方法,实现了在网页上输入指定内容的操作。

WebElement element = driver.findElement(By.id("passwd-id"));

 

在输入框中输入内容:

 

element.sendKeys(“test”);

 

all basic functions in RemoteWebElement

from seleniumwire import webdriver import unittest from jiaoben.login.action.page_login import LoginPage from selenium.webdriver.chrome.options import Options class Login(unittest.TestCase): def setUp(self): options = Options() options.add_experimental_option("detach", True) # 关键配置 self.driver = webdriver.Chrome(options=options) self.driver.get("https://10.121.177.160") def test_login(self): print(f"开始登录") LoginPage(self.driver).input_key(user='admin', passwd='admin') # assert self.driver.title in "Chaoqiang G640 V7 10.121.176.137" print("登录成功") from selenium.webdriver.common.by import By from jiaoben.Method_Library.base_action import BaseAction class LoginPage(BaseAction): user = (By.ID,"userid") passwd = (By.ID,"password") title_list = (By.XPATH, '//*[@id="btn-login"]') def input_key(self, user, passwd): self.input(self.user, user) self.input(self.passwd, passwd) self.find_element(self.title_list).click() import time from pyexpat import features from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.common.alert import Alert class BaseAction: def __init__(self, driver): # 将传入的 driver 实例赋值给类的实例变量,供后续方法使用 self.driver = driver cookies = self.driver.get_cookies() #将cookies添加到浏览器当中,保持登录状态 for i in cookies: self.driver.add_cookie(i) #刷新页面,使添加的cookies生效 self.driver.refresh() def find_element(self, feature, timeout=10, poll=1): return WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_element(*feature)) def click(self, feature, timeout=10, poll=1): self.find_element(feature, timeout, poll).click() def input(self, feature, text, timeout=10, poll=1): self.find_element(feature, timeout, poll).send_keys(text) def clear_and_input(self, feature, text, timeout=10, poll=1): self.clear(feature, timeout, poll) self.find_element(feature, timeout, poll).send_keys(text) def clear(self, feature, timeout=10, poll=1): self.find_element(feature, timeout, poll).clear() def get_text(self, feature, timeout=10, poll=1): return self.find_element(feature, timeout, poll).text def get(self, feature, timeout=10, poll=1): return self.find_element(feature, timeout, poll) def switch_new_page(self): self.driver.switch_to.window(self.driver.window_handles[-1]) def switch_old_page(self): self.driver.switch_to.window(self.driver.window_handles[0]) def find_elements(self, feature, timeout=10, poll=1): return WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_elements(*feature)) def get_list(self, feature, timeout=10, poll=1): return self.find_elements(feature, timeout, poll) def move(self, feature, timeout=10, poll=1): webElement = self.get(feature, timeout, poll) self.driver.execute_script("arguments[0].scrollIntoView();",webElement) def move_to(self, feature, timeout=10, poll=1): webElement = self.find_element(feature, timeout, poll) ActionChains(self.driver).move_to_element(webElement).perform() def toset(self): alert = Alert(self.driver) alert.accept() # 点击确定/OK按钮 time.sleep(10) # 通用属性获取 def get_value(self, feature, attribute_name, timeout=10, poll=1): return self.find_element(feature, timeout, poll).get_attribute(attribute_name) 为什么会使得登录界面执行成功后一直停留在该页面
最新发布
08-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值