Tag Archives: selenium

Selenium 中ExpectedConditions 用法说明(最全整理)

首先导入包 [cc lang=”python”]from selenium.webdriver.support import expected_conditions as EC[/cc] 1,判断当前页面的title是否精确等于预期 [cc lang=”text”]titleIs( String title) EC.title_is(‘Google’) [/cc] 2,判断当前页面的title是否包含预期字符串 [cc lang=”text”]titleContains( String title)[/cc] [cc lang=”python”]if EC.title_contains(‘google’): ##等于 if ‘google’ in driver.title: [/cc] 3,判断当前页面的url是否精确等于预期 [cc lang=”text”]urlToBe( String url)[/cc] 4,判断当前页面的url是否包含预期字符串 [cc lang=”text”]urlContains( String fraction)[/cc] 5,当前URL字符串正则表达式匹配 [cc lang=”text”]urlMatches( String regex)[/cc] 6,判断元素是否出现,只要有一个元素出现,就通过。(出现不代表可见) 判断是否至少有 1 个元素存在于 dom 树中。举个例子,如果页面上有 n 个元素的 class 都是’column-md-3’,那么只要有 1… Read More »

Python Selenium提供的execute_script调用js的一些操作

调用js方法 execute_script(script, *args) 在当前窗口/框架 同步执行javaScript 脚本:JavaScript的执行。 *参数:适用任何JavaScript脚本。 使用: [cc lang=”python”]driver.execute_script(‘document.title’)[/cc] 滚动到目标视图 [cc lang=”python”]target = driver.find_element_by_xxxx() driver.execute_script(“arguments[0].scrollIntoView();”, target)[/cc] 通过执行JavaScript中的代码删除target JS code [cc lang=”js”]document.getElementsByClassName(“site-nav-right fr”)[0].childNodes[1].removeAttribute(“target”)[/cc] python [cc lang=”python”]js=’arguments[0].removeAttribute(argument[1])’ driver.execute_script(js,login_link,”target”)[/cc]

Selenium:利用select模块处理下拉框

在利用selenium进行UI自动化测试过程中,经常会遇到下拉框选项,这篇博客,就介绍下如何利用selenium的Select模块来对标准select下拉框进行操作。。。 首先导入Select模块: [cc lang=”python”] # coding=utf-8 from selenium import webdriver from selenium.webdriver.support.select import Select [/cc] 1、Select提供了三种选择某一项的方法 [cc lang=”python”] select_by_index # 通过索引定位 select_by_value # 通过value值定位 select_by_visible_text # 通过文本值定位 [/cc] 注意事项: index索引是从“0”开始; value是option标签的一个属性值,并不是显示在下拉框中的值; visible_text是在option标签中间的值,是显示在下拉框的值; 2、Select提供了三种返回options信息的方法 [cc lang=”python”] options # 返回select元素所有的options all_selected_options # 返回select元素中所有已选中的选项 first_selected_options # 返回select元素中选中的第一个选项[/cc] 注意事项: 这三种方法的作用是查看已选中的元素是否是自己希望选择的: options:提供所有选项的元素列表; all_selected_options:提供所有被选中选项的元素列表; first_selected_option:提供第一个被选中的选项元素; 3、Select提供了四种取消选中项的方法 [cc lang=”python”] deselect_all # 取消全部的已选择项… Read More »

如何正确移除Selenium中的 window.navigator.webdriver

Chrome 79.0.3945.36+ [cc lang=”python”]from selenium import webdriver options = webdriver.ChromeOptions() options.add_experimental_option(“excludeSwitches”, [“enable-automation”]) options.add_experimental_option(‘useAutomationExtension’, False) driver = webdriver.Chrome(options=options, executable_path=’./chromedriver’) driver.execute_cdp_cmd(“Page.addScriptToEvaluateOnNewDocument”, { “source”: “”” Object.defineProperty(navigator, ‘webdriver’, { get: () = undefined }) “”” }) driver.get(‘http://google.com’)[/cc] Chrome 旧版本 [cc lang=”python”]from selenium.webdriver import Chrome from selenium.webdriver import ChromeOptions option = ChromeOptions() option.add_experimental_option(‘excludeSwitches’,[‘enable-automation’]) driver =Chrome(options=option)[/cc]