首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Selenium系列学习(一)

Selenium系列学习(一)

作者头像
问问计算机
发布2021-05-08 11:19:11
6670
发布2021-05-08 11:19:11
举报
文章被收录于专栏:问问计算机问问计算机

Selenium官方说明文档

  • 英文-https://www.selenium.dev/documentation/en/
  • 中文-https://www.selenium.dev/documentation/zh-cn/getting_started_with_webdriver/

Selenium浏览器自动化项目

Selenium is an umbrella project for a range of tools and libraries that enabled and support the automation of web browsers.

这里说的"umbrella project"是一个比较关键的概念, 就是说,它是一个 ”一揽子“工程,包含了系列内容,不是指某个单独的独立项目。

Selenium的核心是WebDriver,它是编写指令集的接口,该指令集可以在许多浏览器中交互。

  • WebDriver

如果您开始使用桌面网站测试自动化, 那么您将使用 WebDriver APIs. WebDriver 使用浏览器供应商提供的浏览器自动化 API 来控制浏览器和运行测试. 这就像真正的用户正在操作浏览器一样. 由于 WebDriver 不要求使用应用程序代码编译其 API, 因此它本质上不具有侵入性. 因此, 您测试的应用程序与实时推送的应用程序相同.

  • IDE

Selenium IDE (Integrated Development Environment 集成开发环境) 是用来开发 Selenium 测试用例的工具. 这是一个易于使用的 Chrome 和 Firefox 浏览器扩展, 通常是开发测试用例最有效率的方式. 它使用现有的 Selenium 命令记录用户在浏览器中的操作, 参数由元素的上下文确定. 这不仅节省了开发时间, 而且是学习 Selenium 脚本语法的一种很好的方法.

  • Grid

Selenium Grid允许您在不同平台的不同机器上运行测试用例. 可以本地控制测试用例的操作, 当测试用例被触发时, 它们由远端自动执行.

当开发完WebDriver测试之后, 您可能需要在多个浏览器和操作系统的组合上运行测试. 这就是 Grid 的用途所在.

            using (IWebDriver driver = new FirefoxDriver())
            {
                WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
                driver.Navigate().GoToUrl("https://www.google.com/ncr");
                driver.FindElement(By.Name("q")).SendKeys("cheese" + Keys.Enter);
                IWebElement firstResult = wait.Until(ExpectedConditions.ElementExists(By.CssSelector("h3>div")));
                Console.WriteLine(firstResult.GetAttribute("textContent"));
            }

在示例代码中存在ExpectedConditions.ElementExists,第一次使用的时候,即使安装了WebDriver和WebDriver.Support,依然无法找到ExpectedConditions,搜索后发现,需要使用SeleniumExtras.WaitHelpers,所以完整的内容如下:

当启动运行后会报出如下内容的异常:

OpenQA.Selenium.DriverServiceNotFoundException
  HResult=0x80131500
  Message=The geckodriver.exe file does not exist in the current directory or in a directory on the PATH environment variable. The driver can be downloaded at https://github.com/mozilla/geckodriver/releases.
  Source=WebDriver
  StackTrace:
   在 OpenQA.Selenium.DriverService.FindDriverServiceExecutable(String executableName, Uri downloadUrl)
   在 OpenQA.Selenium.Firefox.FirefoxDriverService.CreateDefaultService()
   在 OpenQA.Selenium.Firefox.FirefoxDriver.CreateService(FirefoxOptions options)
   在 OpenQA.Selenium.Firefox.FirefoxDriver..ctor(FirefoxOptions options)
   在 OpenQA.Selenium.Firefox.FirefoxDriver..ctor()
   在 ConApp***.Service.SeleniumHelper.Test() 在 G:\***.cs 中: 第 17 行
   在 ConApp***.Program.Main(String[] args) 在 G:\***.cs 中: 第 24 行

缺少geckodriver.exe的可执行文件,按照异常提示中的内容,找到geckodriver:

(图片来自:https://github.com/mozilla/geckodriver/releases)

下载后放在对应的编译目录下或者环境变量中。

随后又会出现如下的异常提示:

System.InvalidOperationException
  HResult=0x80131509
  Message=Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line (SessionNotCreated)
  Source=WebDriver
  StackTrace:
   在 OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
   在 OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   在 OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
   在 OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
   在 OpenQA.Selenium.Firefox.FirefoxDriver..ctor(FirefoxDriverService service, FirefoxOptions options, TimeSpan commandTimeout)
   在 OpenQA.Selenium.Firefox.FirefoxDriver..ctor(FirefoxOptions options)
   在 OpenQA.Selenium.Firefox.FirefoxDriver..ctor()
   在 ***.Test() 在 G:\***.cs 中: 第 17 行
   在 ***.Main(String[] args) 在 G:\***\Program.cs 中: 第 24 行

没有找到二进制文件,什么是二进制文件?

(图片来自:https://baike.baidu.com/item/%E4%BA%8C%E8%BF%9B%E5%88%B6%E6%96%87%E4%BB%B6/996661?fromtitle=Binary%20File&fromid=1889287&fr=aladdin)

安装完Firefox浏览器后再次执行程序:

1593877734297   mozrunner::runner       INFO    Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-foreground" "-no-remote" "-profile" "C:\\Users\\***~1\\AppData\\Local\\Temp\\rust_mozprofileLZuStv"

提示“permission denied”,虽然提示“没有权限”,实际上原因是,无法访问Google导致。当把域名修改为https://www.baidu.com 后能够发现,程序能够正常访问并运行。

到这里基于Selenium调用浏览器并访问对应域名的过程基本完成了。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-07-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 changyandou 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档