与网页元素交互
用于操作表单控件的高级指令集。
只能在元素上执行 5 个基本命令
其他验证
这些方法旨在密切模仿用户的体验,因此,与Actions API不同,它在尝试执行指定操作之前会尝试执行两件事。
- 如果它确定元素在视口之外,它会将元素滚动到视图中,具体来说,它将元素的底部与视口的底部对齐。
- 它确保元素在执行操作之前是可交互的。这可能意味着滚动不成功,或者元素没有以其他方式显示。确定元素是否显示在页面上太难了,无法在webdriver 规范中直接定义,因此 Selenium 发送一个带有 JavaScript 原子的执行命令,检查是否有一些东西会阻止元素显示。如果它确定元素不在视口中、未显示、不是键盘可交互的或不是指针可交互的,它会返回一个元素不可交互错误。
点击
元素点击命令在元素的中心执行。如果元素的中心由于某种原因被遮挡,Selenium 将返回一个元素点击被拦截错误。
driver.get("https://seleniumcn.cn/selenium/web/inputs.html");
// Click on the element
WebElement checkInput=driver.findElement(By.name("checkbox_input"));
checkInput.click();
# Navigate to URL
driver.get("https://seleniumcn.cn/selenium/web/inputs.html")
# Click on the checkbox
check_input = driver.find_element(By.NAME, "checkbox_input")
check_input.click()
// Navigate to Url
driver.Navigate().GoToUrl("https://seleniumcn.cn/selenium/web/inputs.html");
// Click on the element
IWebElement checkInput = driver.FindElement(By.Name("checkbox_input"));
checkInput.Click();
driver.find_element(name: 'color_input').click
await submitButton.click();
// Navigate to Url
driver.get("https://seleniumcn.cn/selenium/web/inputs.html")
// Click the element
driver.findElement(By.name("color_input")).click();
发送键
元素发送键命令将提供的键输入到可编辑元素中。通常,这意味着元素是 text
类型的表单的输入元素,或者具有 content-editable
属性的元素。如果它是不可编辑的,则会返回无效元素状态错误。
这里是 WebDriver 支持的可能按键列表。
// Clear field to empty it from any previous data
WebElement emailInput=driver.findElement(By.name("email_input"));
emailInput.clear();
//Enter Text
String email="admin@localhost.dev";
emailInput.sendKeys(email);
# Handle the email input field
email_input = driver.find_element(By.NAME, "email_input")
email_input.clear() # Clear field
email = "admin@localhost.dev"
email_input.send_keys(email) # Enter text
//SendKeys
// Clear field to empty it from any previous data
IWebElement emailInput = driver.FindElement(By.Name("email_input"));
emailInput.Clear();
//Enter Text
String email = "admin@localhost.dev";
emailInput.SendKeys(email);
driver.find_element(name: 'email_input').send_keys 'admin@localhost.dev'
let inputField = await driver.findElement(By.name('no_type'));
// Navigate to Url
driver.get("https://seleniumcn.cn/selenium/web/inputs.html")
//Clear field to empty it from any previous data
driver.findElement(By.name("email_input")).clear()
// Enter text
driver.findElement(By.name("email_input")).sendKeys("admin@localhost.dev")
清除
元素清除命令重置元素的内容。这要求元素是可编辑的,并且是可重置的。通常,这意味着元素是 text
类型的表单的输入元素,或者具有 content-editable
属性的元素。如果这些条件不满足,则会返回无效元素状态错误。
//Clear Element
// Clear field to empty it from any previous data
emailInput.clear();
email_input.clear()
//Clear Element
// Clear field to empty it from any previous data
emailInput.Clear();
data = emailInput.GetAttribute("value");
driver.find_element(name: 'email_input').clear
await driver.get('https://seleniumcn.cn/selenium/web/inputs.html');
// Navigate to Url
driver.get("https://seleniumcn.cn/selenium/web/inputs.html")
//Clear field to empty it from any previous data
driver.findElement(By.name("email_input")).clear()
提交
在 Selenium 4 中,这不再使用单独的端点实现,而是通过执行脚本来工作。因此,建议不要使用此方法,而是单击适用的表单提交按钮。