测试人社区

selenium-在 AUT* 中执行


title: “在 AUT* 中执行”
weight: 4

使用 sendKeys 方法设置元素的文本,如下所示:

{{< code-tab >}}
{{< code-panel language=“java” >}}
String name = “Charles”;
driver.findElement(By.name(“name”)).sendKeys(name);
{{< / code-panel >}}
{{< code-panel language=“python” >}}
name = “Charles”
driver.find_element(By.NAME, “name”).send_keys(name)
{{< / code-panel >}}
{{< code-panel language=“csharp” >}}
string name = “Charles”;
driver.FindElement(By.Name(“name”)).SendKeys(name);
{{< / code-panel >}}
{{< code-panel language=“ruby” >}}
name = “Charles”
driver.find_element(name: “name”).send_keys(name)
{{< / code-panel >}}
{{< code-panel language=“javascript” >}}
const name = “Charles”;
await driver.findElement(By.name(‘name’)).sendKeys(name);
{{< / code-panel >}}
{{< code-panel language=“kotlin” >}}
val name = “Charles”
driver.findElement(By.name(“name”)).sendKeys(name)
{{< / code-panel >}}
{{< / code-tab >}}

一些 web 应用程序使用 JavaScript 库来实现拖放功能。
下面是一个简单的例子,拖动一个元素到另一个元素:

{{< code-tab >}}
{{< code-panel language=“java” >}}
WebElement source = driver.findElement(By.id(“source”));
WebElement target = driver.findElement(By.id(“target”));
new Actions(driver).dragAndDrop(source, target).build().perform();
{{< / code-panel >}}
{{< code-panel language=“python” >}}
source = driver.find_element(By.ID, “source”)
target = driver.find_element(By.ID, “target”)
ActionChains(driver).drag_and_drop(source, target).perform()
{{< / code-panel >}}
{{< code-panel language=“csharp” >}}
IWebElement source = driver.FindElement(By.Id(“source”));
IWebElement target = driver.FindElement(By.Id(“target”));
new Actions(driver).DragAndDrop(source, target).Build().Perform();
{{< / code-panel >}}
{{< code-panel language=“ruby” >}}
source = driver.find_element(id: “source”)
target = driver.find_element(id: “target”)
driver.action.drag_and_drop(source, target).perform
{{< / code-panel >}}
{{< code-panel language=“javascript” >}}
const actions = driver.actions({bridge: true});
const source = driver.findElement(By.id(‘source’));
const target = driver.findElement(By.id(‘target’));
await actions.dragAndDrop(source, target).perform();
{{< / code-panel >}}
{{< code-panel language=“kotlin” >}}
val source = driver.findElement(By.id(“source”))
val target = driver.findElement(By.id(“target”))
Actions(driver).dragAndDrop(source, target).build().perform()
{{< / code-panel >}}
{{< / code-tab >}}

单击元素

你可以使用 click 方法单击一个元素:

{{< code-tab >}}
{{< code-panel language=“java” >}}
driver.findElement(By.cssSelector(“input[type=‘submit’]”)).click();
{{< / code-panel >}}
{{< code-panel language=“python” >}}
driver.find_element(By.CSS_SELECTOR, “input[type=‘submit’]”).click()
{{< / code-panel >}}
{{< code-panel language=“csharp” >}}
driver.FindElement(By.CssSelector(“input[type=‘submit’]”)).Click();
{{< / code-panel >}}
{{< code-panel language=“ruby” >}}
driver.find_element(css: “input[type=‘submit’]”).click
{{< / code-panel >}}
{{< code-panel language=“javascript” >}}
await driver.findElement(By.css(“input[type=‘submit’]”)).click();
{{< / code-panel >}}
{{< code-panel language=“kotlin” >}}
driver.findElement(By.cssSelector(“input[type=‘submit’]”)).click()
{{< / code-panel >}}
{{< / code-tab >}}

*AUT: 待测试应用

官方链接为:https://www.selenium.dev/documentation/zh-cn/getting_started_with_webdriver/performing_actions_on_the_aut

官方链接为:https://www.selenium.dev/documentation/zh-cn/getting_started_with_webdriver/performing_actions_on_the_aut