{"id":19545146,"url":"https://github.com/scscms/scs-webdriver","last_synced_at":"2025-04-26T19:31:57.140Z","repository":{"id":93555101,"uuid":"140558861","full_name":"scscms/scs-webdriver","owner":"scscms","description":"Selenium WebDriver can drive a browser natively either locally or on remote machines. ","archived":false,"fork":false,"pushed_at":"2018-07-11T10:26:49.000Z","size":3843,"stargazers_count":5,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-04T17:21:46.772Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/scscms.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-07-11T10:20:01.000Z","updated_at":"2020-11-22T23:18:10.000Z","dependencies_parsed_at":"2023-10-02T08:03:10.205Z","dependency_job_id":null,"html_url":"https://github.com/scscms/scs-webdriver","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scscms%2Fscs-webdriver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scscms%2Fscs-webdriver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scscms%2Fscs-webdriver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scscms%2Fscs-webdriver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/scscms","download_url":"https://codeload.github.com/scscms/scs-webdriver/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251041453,"owners_count":21527198,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T03:35:37.561Z","updated_at":"2025-04-26T19:31:57.134Z","avatar_url":"https://github.com/scscms.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Selenium-webdriver常用总结\u003csup\u003emonitor\u003c/sup\u003e\n\n    Selenium WebDriver can drive a browser natively either locally or on remote machines. \n\n### 准备\n\n- node.js 的安装和配置(略)\n- 安装Selenium-webdriver\n```\nnpm install -save selenium-webdriver\n```\n- 驱动下载：\n\n  - `chromedriver`[http://chromedriver.storage.googleapis.com/index.html](http://chromedriver.storage.googleapis.com/index.html)\n  - `IEDriverServer`[http://selenium-release.storage.googleapis.com/index.html](http://selenium-release.storage.googleapis.com/index.html)\n  - `geckodriver`[https://github.com/mozilla/geckodriver/releases/](https://github.com/mozilla/geckodriver/releases/)\n\nchrome,IE的驱动都与当前浏览器有关，必须匹配才能使用。在此我仅以 `Firefox` 来测试。\n\n### 使用\n\n#### 一、基本使用\n\n```js\nconst {Builder, By, Key, until, Button} = require(\"selenium-webdriver\");\nlet broswer = new Builder().forBrowser('ie').build()  //这里使用了ie引擎\nbroswer.get('http://www.baidu.com')\nbroswer.quit()  // 表示关闭浏览器   \n//drive.close()表示关闭当前窗口\n```\n\n#### 二、选择器\n\n```js\nbroswer.findElement(By.name('btn'))\nbroswer.findElement({id:\"btn\"})\nelement.findElement() //同样可以对元素使用findElement方法\nbroswer.findElements() //查找多个元素\n\nBy.className(classname)\nBy.css(selector)   //css-selector\nBy.id(id)\nBy.name(name)\nBy.linkText(text)\nBy.partialLink(text)  \nBy.xpath()   \nBy.js()\n```\n\n#### 三、属性获取\n\n```js\n//获取代码：\nbrowser.getPageSource().then(function(souce) {console.log(souce)});\n//获取网页标题:\nbrowser.getTitle().then(b=\u003e{console.log(b)});\n//获取当前url：\nbrowser.getCurrentUrl().then(b=\u003e{console.log(b)});\n\n//element为web元素对象，为findelement()的返回对象\nelement.getText().then(b=\u003e{console.log(\"text\",b)}) //返回里面没有被隐藏的文字（不带标签）\n\nelement.getTagName().then(b=\u003e{console.log(\"Tagname\",b)})//返回标签名\n\nelement.getId().then(b=\u003e{console.log(\"ID\",b)})//返回这个element服务器分配的不透明id\n\nelement.getCssValue().then(()=\u003e{console.log(\"CSSvalue\",b)})//返回该element的CSS属性\n\n//其他属性:\nelement.getAttribute(\"class\").then(b=\u003e{console.log(b)})\n```\n\n#### 四、等待\n\n```js\n//等待元素：\nbrowser.wait(until.elementLocated(By.id('foo')), 10000);\n\nbrowser.wait(function() {\n    return driver.getTitle().then(function(title) {\n        console.log(11111111);\n        return title === 'webdriver - Google Search';\n    });\n}, 1000);\n\nbrowser.wait(until.titleIs('webdriver - Google Search'), 1000)\n\n//settimeouts\nbrowser.manage().setTimeouts(args)\n// args参数  {implicit: (number|null|undefined), pageLoad: (number|null|undefined), script: (number|null|undefined)}  implicit：等待元素加载的最大时间；pageLoad等待页面加载完成的最大时间\n```\n\n#### 五、操作\n\n- input操作\n\n```js\nelement.clear();//清空\n\n//输入 \nelement.sendKeys(\"webdriver\");\nelement.sendKeys(Key.ENTER); //按回车\nelement.submit();  //以submit命令提交执行\n```\n\n- 截图\n\n```js\nbroswer.takeScreenshot().then()  //返回页面png截图\nelement.takeScreenshoot().then() //返回元素png截图\n```\n\n- 鼠标操作\n\n```js\n//单击鼠标\nelement.click() \n//连锁动作(action对象)\nconst actions = driver.actions();\nactions\n     .keyDown(SHIFT)\n     .move({origin: el})\n     .press()\n     .release()\n     .keyUp(SHIFT)\n     .perform();\n//actions对象以perform()作为动作链结尾，表示命令执行\n\n\n//具体方法如下：\nactions.clear() //清空所有动作，按键和状态\nactions.click(element) //对element左键单击一次\nactions.contextClick(element) //对element右键单击一次\nactions.doubleClick(element)  //对element双击一次\nactions.dragAndDrop(ele_from,to) //单击鼠标拖动ele_from元素，如果to是坐标{x:number,y:number}移动距离;如果to是元素移动到to元素中心，并释放鼠标。\n\n\nactions.keyDown(key) //按下键盘的key键\nactions.keyUp(key)  //释放key键\n\nactions.move(options) //移动参数如下：  \n//options ({duration: (number|undefined), origin: (Origin|WebElement|undefined), x: (number|undefined), y: (number|undefined)}|undefined) \n//origin是起始位置，默认为鼠标当前位置，可以设置元素为起始位置。x,y为偏移量。duration为持续时间默认（100ms）\n\nactions.press(button) //按下鼠标 button默认是鼠标左键，有LEFT,RIGHT,MIDDLE三个值，通过Button.LEFT....获得\nactions.release(button) //释放鼠标，默认左键\nactions.sendKeys()\nactions.pause(ms,devices) //暂停ms时间，如果devices没有指定，会创建一个针对所有的事件\n```\n\n#### 六、options对象\n\n```js\n//通过let options = broswer.manage() 获得\n\noptions.addCookie({name: 'foo', value: 'bar'})\noptions.deleteAllCookies() //删除所有cookies\noptions.deleteCookie(name) //按照name删除\noptions.getCookie(name) //拿到name字段的cookie值，为promise对象\noptions.getCookies() //返回所有cookies，为promise对象\n```\n\n#### 七、nav对象\n\n通过let nav = broswer.navigate()获得nav有四个方法分别为：\n```js\n　　nav.back();\n　　nav.forward();\n　　nav.refresh();\n　　nav.to(url);\n```\n\n#### 八、其他\n\n- ①browser.excuteScript(script) //在当前frame中执行js代码\n- ②brower.switchTo() //targetlocator\n\np.s. 根据浏览器版本和引擎不同，部分方法存在问题。\n\n注意：一些执行操作的promise对象，需要合理利用async和await方法\n\n[查看更多API](http://seleniumhq.github.io/selenium/docs/api/javascript/index.html)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscscms%2Fscs-webdriver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fscscms%2Fscs-webdriver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscscms%2Fscs-webdriver/lists"}