编写第一个Selenium脚本 您所在的位置:网站首页 自动化脚本编写规范要求 编写第一个Selenium脚本

编写第一个Selenium脚本

2024-04-11 20:11| 来源: 网络整理| 查看: 265

编写第一个Selenium脚本逐步构建一个Selenium脚本的说明

当你完成 Selenium安装 后, 便可以开始书写Selenium脚本了.

八个基本组成部分

Selenium所做的一切, 就是发送给浏览器命令, 用以执行某些操作或为信息发送请求. 您将使用Selenium执行的大部分操作, 都是以下基本命令的组合

Click on the link to “View full example on GitHub” to see the code in context.

1. 使用驱动实例开启会话

关于如何启动会话,请浏览我们的文档 驱动会话

WebDriver driver = new ChromeDriver(); View full example on GitHubdriver = webdriver.Chrome() View full example on GitHub IWebDriver driver = new ChromeDriver(); View full example on GitHubdriver = Selenium::WebDriver.for :chrome View full example on GitHub driver = await new Builder().forBrowser(Browser.CHROME).build(); View full example on GitHub driver = ChromeDriver() View full example on GitHub2. 在浏览器上执行操作

在本例中, 我们 导航 到一个网页.

driver.get("https://www.selenium.dev/selenium/web/web-form.html"); View full example on GitHubdriver.get("https://www.selenium.dev/selenium/web/web-form.html") View full example on GitHub driver.Navigate().GoToUrl("https://www.selenium.dev/selenium/web/web-form.html"); View full example on GitHubdriver.get('https://www.selenium.dev/selenium/web/web-form.html') View full example on GitHub await driver.get('https://www.selenium.dev/selenium/web/web-form.html'); View full example on GitHub driver.get("https://www.selenium.dev/selenium/web/web-form.html") View full example on GitHub3. 请求 浏览器信息

您可以请求一系列关于浏览器的信息 , 包括窗口句柄、浏览器尺寸/位置、cookie、警报等.

driver.getTitle(); View full example on GitHubtitle = driver.title View full example on GitHub var title = driver.Title; View full example on GitHubdriver.title View full example on GitHub let title = await driver.getTitle(); View full example on GitHub val title = driver.title View full example on GitHub4. 建立等待策略

将代码与浏览器的当前状态同步 是Selenium面临的最大挑战之一, 做好它是一个高级主题.

基本上, 您希望在尝试定位元素之前, 确保该元素位于页面上, 并且在尝试与该元素交互之前, 该元素处于可交互状态.

隐式等待很少是最好的解决方案, 但在这里最容易演示, 所以我们将使用它作为占位符.

阅读更多关于等待策略 的信息.

driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500)); View full example on GitHubdriver.implicitly_wait(0.5) View full example on GitHub driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(500); View full example on GitHubdriver.manage.timeouts.implicit_wait = 500 View full example on GitHub await driver.manage().setTimeouts({implicit: 500}); View full example on GitHub driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500)) View full example on GitHub5. 发送命令 查找元素

大多数Selenium会话中的主要命令都与元素相关, 如果不先找到元素, 就无法与之交互.

WebElement textBox = driver.findElement(By.name("my-text")); WebElement submitButton = driver.findElement(By.cssSelector("button")); View full example on GitHubtext_box = driver.find_element(by=By.NAME, value="my-text") submit_button = driver.find_element(by=By.CSS_SELECTOR, value="button") View full example on GitHub var textBox = driver.FindElement(By.Name("my-text")); var submitButton = driver.FindElement(By.TagName("button")); View full example on GitHubtext_box = driver.find_element(name: 'my-text') submit_button = driver.find_element(tag_name: 'button') View full example on GitHub let textBox = await driver.findElement(By.name('my-text')); let submitButton = await driver.findElement(By.css('button')); View full example on GitHub var textBox = driver.findElement(By.name("my-text")) val submitButton = driver.findElement(By.cssSelector("button")) View full example on GitHub6. 操作元素

对于一个元素, 只有少数几个操作可以执行, 但您将经常使用它们.

textBox.sendKeys("Selenium"); submitButton.click(); View full example on GitHubtext_box.send_keys("Selenium") submit_button.click() View full example on GitHub textBox.SendKeys("Selenium"); submitButton.Click(); View full example on GitHubtext_box.send_keys('Selenium') submit_button.click View full example on GitHub await textBox.sendKeys('Selenium'); await submitButton.click(); View full example on GitHub textBox.sendKeys("Selenium") submitButton.click() View full example on GitHub7. 获取元素信息

元素存储了很多被请求的信息.

message.getText(); View full example on GitHubtext = message.text View full example on GitHub var value = message.Text; View full example on GitHubmessage.text View full example on GitHub let value = await message.getText(); View full example on GitHub val value = message.getText() View full example on GitHub8. 结束会话

这将结束驱动程序进程, 默认情况下, 该进程也会关闭浏览器. 无法向此驱动程序实例发送更多命令.

See Quitting Sessions.

driver.quit(); View full example on GitHubdriver.quit() View full example on GitHub driver.Quit(); View full example on GitHubdriver.quit View full example on GitHub await driver.quit(); View full example on GitHub driver.quit() View full example on GitHubRunning Selenium File

Add Example

Add Example

Add Example

ruby example_script.rb View full example on GitHubnode example_script.spec.js View full example on GitHub

Add Example

接下来的步骤

Most Selenium users execute many sessions and need to organize them to minimize duplication and keep the code more maintainable. Read on to learn about how to put this code into context for your use case with Using Selenium.

最后修改 April 8, 2024: Javascript single test file execution (#1663)[deploy site] (c2299d8a165)


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有