使用Selenium进行测试自动化已使全球的网站测试人员能够轻松执行自动化的网站测试。 Webdriver是Selenium框架的核心组件,通过它您可以针对不同类型的浏览器(例如Google Chrome,Mozilla Firefox,Safari,Opera,Internet Explorer,Microsoft Edge等)对网站或Web应用程序执行自动跨浏览器测试 。

与其他Web自动化工具/框架相比,使用Selenium Webdriver执行测试自动化的主要优势是支持多种编程语言,例如Python,Java,C#,Ruby,PHP,JavaScript,.Net,Perl等。是Selenium WebDriver自动化测试的基础知识,然后您可以查看我们的Selenium WebDriver自动化跨浏览器测试教程,在此我们讨论Selenium的总体体系结构以及如何将该框架与流行的编程语言一起使用。 您还可以查看我以前在Selenium Grid设置教程中进行的跨浏览器测试文章 ,以利用Selenium进行并行测试的能力。 无论使用哪种编程语言,都有某些最佳实践适用于使用Selenium Webdriver(独立于开发语言)执行测试自动化。

在本文中,我将与您分享一些Selenium自动化测试的关键技巧,这些技巧涉及代码优化,性能改进,动态网页加载,处理CSS和HTML代码等方面。

–这些用于Selenium WebDriver的自动化测试的编码技巧中的大多数都是通用的,并且可以与开发测试脚本所使用的编程语言无关地应用。 但是,为了进行下面的演示,我们将Selenium与Python语言结合使用。

Selenium技巧1 –设置Selenium Webdriver的可执行路径

为了与被测浏览器进行通信,您需要首先从其官方网站下载相应的插件/ webdriver。 该插件将负责与浏览器进行通信,并且该插件应存在于您正在开发测试的计算机上。 插件/ webdriver路径必须在Selenium Webdriver配置中设置。

尽管可以将插件/ Webdriver放置在任何位置,因为您可以在Selenium Webdriver配置中提供静态/相对路径,但是这种方法容易出错,并且需要跟踪文件路径。 更好和更可靠的方法是将相应的Selenium Webdriver放置在驱动程序可执行文件所在的位置,在这种情况下,您无需在Selenium Webdriver配置中指定可执行文件路径。


如果geckodriver在浏览器位置中不存在,则需要在源代码中手动添加相同的路径。 我们导入selenium.webdriver.firefox.firefox_binary模块以提供Firefox可执行文件的路径。

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinaryff_binary = FirefoxBinary('path/to/gecko driver')
browser = webdriver.Firefox(firefox_binary=ff_binary)

如下面的代码片段所示,由于壁虎驱动程序(Firefox Webdriver)放置在与Firefox浏览器相同的位置,因此我们未指定其位置。 与前一种方法相比,这是一种更可靠的方法,可以帮助减少使用Selenium实现测试自动化时的基本错误。

''' Import the required modules for development '''
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep'''Creation of Firefox Webdriver '''
driver = webdriver.Firefox()
driver.get("https://www.lambdatest.com/")

Selenium技巧2 –使用Selenium WebDriver捕获测试自动化的屏幕截图

在执行测试时,您会遇到一些要求,其中必须捕获屏幕快照以验证测试结果。 Selenium WebDriver提供了三种API,您可以通过它们获取网页的屏幕截图。

  1. save_screenshot('应该保存屏幕快照的路径/filename.png')
  2. get_screenshot_as_file('应该保存屏幕快照的路径/filename.png')
  3. get_screenshot_as_png()

前两个API可让您将当前窗口的屏幕保存为.png文件。 如果存在IOError,则API返回False,否则返回True。 仅当文件扩展名为.png时,这些API才有效,否则Python会引发错误并且保存的内容可能无法查看。 如果您希望以二进制格式捕获当前窗口的屏幕,请使用get_screenshot_as_png()API。

''' Import the required modules for development '''
from selenium import webdriver
import StringIO
from PIL import Image'''Creation of Firefox Webdriver '''
driver = webdriver.Firefox()
driver.get("https://www.lambdatest.com/")'''Taking screenshot of the web-page. File would be saved in the location where the source code is present ''''''Option - 1'''
driver.save_screenshot('screenshot_1.png');'''Option - 2'''
driver.get_screenshot_as_file('screenshot_2.png');
'''Option - 3'''
screenshot = driver.get_screenshot_as_png();screenshot_size = (20, 10, 480, 600)
image = Image.open (StringIO.StringIO(screen))
region = image.crop(screenshot_size)
region.save('screenshot_3.jpg', 'JPEG', optimize=True)

Selenium技巧3 –使用Selenium WebDriver进行自动化测试时刷新网页

在某些情况下,可能需要刷新网页,尤其是在等待特定条件时。 使用Selenium Webdriver执行测试自动化时,有多种方法可以刷新网页,下面列出了一种流行的方法。

1. driver.refresh()方法

顾名思义, refresh()方法用于刷新网页。 因此,它本质上是异步的。 您应该将此API与document.readyState()结合使用。

''' Import the required modules for development '''
from selenium import webdriver'''Creation of Firefox Webdriver '''
driver = webdriver.Firefox()
driver.get("https://www.lambdatest.com/")
driver.refresh()

2. ActionChains()方法

ActionChains()是自动化与Selenium进行自动化测试的低级交互的另一种方式,例如按键,鼠标按钮动作等。为了刷新网页,我们使用了'CTRL + F5'组合。

import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys'''Creation of Firefox Webdriver '''
# driver = webdriver.Chrome()
driver = webdriver.Firefox()
driver.get("https://www.lambdatest.com/")time.sleep(5)print("Before refresh")ActionChains(driver) \.key_down(Keys.CONTROL) \.send_keys(Keys.F5) \.key_up(Keys.CONTROL) \.perform()print("After refresh")sleep(5)
driver.quit()

Selenium技巧4 –在新标签页中打开网页

execute_script可用于在当前窗口/框架中同步执行JavaScript代码。 将打开网页的参数(JavaScript)作为参数传递给execute_script

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleepdriver = webdriver.Firefox()
driver.get("http://www.google.com/")driver.implicitly_wait(10)#open tab
driver.execute_script("window.open('https://www.lambdatest.com', 'new tab')")sleep(5)
driver.quit()

Selenium技巧5 –保存网页的部分屏幕截图

在某些情况下,使用Selenium执行测试自动化时,可能需要截取网页的部分屏幕截图。 在这种情况下,您可以使用枕头模块。 您需要先使用以下命令安装Pillow / PIL模块

pip install pillow

使用get_screenshot_as_png() API拍摄整个网页的屏幕截图。 截图准备好后,将使用PIL库在内存中打开捕获的图像,然后裁剪图像(包含整个网页的屏幕截图)以获取结果图像。

from selenium import webdriver
''' Install the Pillow module using the command pip install pillow '''
from PIL import Image
from io import BytesIOdriver = webdriver.Firefox()
driver.get('http://google.com/')# Use the Inspection tool to find the location of the logo
element = driver.find_element_by_id('hplogo')
image_location = element.location
size = element.sizepng = driver.get_screenshot_as_png()''' Since the webpage screenshot is ready, we can exit the browser.'''
driver.quit()''' PIL Library is used to open the image in memory '''
crop_image = Image.open(BytesIO(png))''' Extract the Left, Right, Top, and Bottom co-ordinates ''' left = image_location['x']
top = image_location['y']
right = image_location['x'] + size['width']
bottom = image_location['y'] + size['height']crop_image = crop_image.crop((left, top, right, bottom))
crop_image.save('logo-screenshot.png')

Selenium技巧6 –执行JavaScript代码

当您使用Selenium WebDriver执行测试自动化时,execute_script用于执行JavaScript代码。 语法为driver.execute_script(“此处JavaScript代码”)


如下例所示,执行Register的on_click操作[类名是home-cta]。

from selenium import webdriver
from time import sleepdriver = webdriver.Firefox()
driver.get("https://www.lambdatest.com")driver.execute_script("document.getElementsByClassName('home-cta')[0].click()")sleep(10)driver.close()

硒技巧#7 –提取JavaScript代码的结果

调用JavaScript代码以使用Selenium进行自动化测试后,您需要提取这些JavaScript代码的结果。 您可以使用return关键字来获取JavaScript代码的结果,如我们在解释JavaScript的扩展示例中所示。

from selenium import webdriver
from time import sleepdriver = webdriver.Firefox()
driver.get("https://www.lambdatest.com")driver.execute_script("document.getElementsByClassName('home-cta')[0].click()")result = driver.execute_script("return 0")
print(result)sleep(10)driver.close()

Selenium技巧8 –处理多种浏览器类型以进行自动跨浏览器测试

您可能需要在多种情况下针对不同的浏览器(例如Firefox,Chrome,Internet Explorer)测试代码。 跨不同浏览器测试网站的做法称为自动浏览器测试 。 要使用Selenium自动化测试执行自动浏览器测试,您应该在单元测试代码或pytest代码中合并对这些浏览器的选择性处理。 下面显示了一个代码片段(利用pytest)来处理多个浏览器:

# Import the 'modules' that are required for executionimport pytest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from time import sleep#Fixture for Firefox
@pytest.fixture(params=["chrome", "firefox"],scope="class")
def driver_init(request):if request.param == "chrome":# Perform necessary actions hereif request.param == "firefox":# Perform necessary actions hereyieldweb_driver.close()......................

Selenium技巧9 –使用CSS定位器在网页上定位元素

使用Selenium执行测试自动化时,在页面上定位Web元素是自动化脚本的基础。 如果您想基于特定种类的Web元素(如Tag,Class,ID等)的存在来执行条件执行,则可以使用find_elements _ *** API。 下面提到其中一些

  • find_elements_by_class_name –按类名称查找元素
  • find_elements –按策略和定位器查找元素
  • find_element_by_link_text –通过链接文本查找元素
  • find_element_by_partial_link_text –通过链接文本的部分匹配来查找元素

下面显示的是find_element_by_partial_link_text和find_elements_by_class_name的用法,其中在受测试的URL https://www.lambdatest.com/上搜索了元素。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.common.exceptions import NoSuchElementExceptiondriver = webdriver.Firefox()
driver.get("https://www.lambdatest.com")try:element = driver.find_element_by_partial_link_text("START TESTING")print("Partial text Element found")element = driver.find_elements_by_class_name('home-btn-2')print("Button Element found")
except NoSuchElementException:print("No element found")sleep(10)
driver.close()

查看我们的博客系列,详细了解用于Selenium测试自动化的各种CSS定位器。

硒技巧#10 – WebElementHTML源

innerHTML属性可用于捕获WebPage的源代码。 自页面首次由网络浏览器加载以来,innerHTML还用于检查页面中的任何更改。 您可以将整个源代码编写为.html文件,以备将来参考。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from time import sleep
import iodriver = webdriver.Firefox()
driver.get("https://www.lambdatest.com")elem = driver.find_element_by_xpath("//*")
source_code = elem.get_attribute("innerHTML")filename = open('lambdatest_page_source.html', 'w')
filename.write(source_code)
filename.close()sleep(10)driver.close()

硒提示11 –将鼠标悬停在动作上

在某些情况下,您可能需要单击作为菜单一部分的项目或作为多级菜单一部分的项目。 首先,我们找到菜单项,然后在所需的菜单项上执行单击操作。

在下面的示例中,被测URL为https://www.lambdatest.com/ 。 目的是导航到主页上的“ 自动化”选项卡 。 第一个任务是找到与ID bs-example-navbar-collapse-1匹配的Menu。 通过使用检查工具,我们可以获得正确的element-id,详细信息如快照中所示


我们使用move_to_element操作移动到菜单,该操作是action_chains模块的一部分。 下一个任务是找到包含文本“ Automation”的菜单项,我们将使用find_element_by_xpath(“ // a [contains(text(),'Automation')]”)))进行单击操作。

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleepdriver = webdriver.Firefox()
driver.get("https://www.lambdatest.com")action = ActionChains(driver);# Head on the top-level menu on Lambdatest website
parent_level_menu = driver.find_element_by_id("bs-example-navbar-collapse-1")
action.move_to_element(parent_level_menu).perform()# Perform a click operation on the Menu item which contains text Automation
child_level_menu = driver.find_element_by_xpath("//a[contains(text(),'Automation')]")
child_level_menu.click();sleep(10)driver.close()

Selenium Tip#12 –关闭标签而不关闭浏览器

对于任何测试自动化Selenium脚本,最基本但必不可少的技巧之一是实现如何在不关闭整个浏览器的情况下关闭选项卡。 driver.close()关闭焦点选项卡, driver.quit()将关闭(浏览器的)所有选项卡,并退出驱动程序。 如果需要使浏览器窗口保持打开状态(并退出所有其他选项卡),则可以使用switch_to.window方法,该方法的输入参数为window handle-id

–还有其他方法可以解决此问题。 window.open方法可以与适当的选项一起使用(例如,打开新窗口,打开新选项卡等)。 可以使用使用send_keys发送正确的按键组合,但是该行为取决于geckodriver版本(对于Firefox),chromedriver版本等。因此, send_keys方法不是可取的,因为输出会根据WebDriver版本而有所不同。

在下面的示例中,我们打开一个包含测试URL的新窗口,然后关闭其他窗口。 我们仅使用window_handles来达到要求。

from selenium import webdriver
import timedriver = webdriver.Firefox()
driver.get('https://www.google.com')
# Open a new window
driver.execute_script("window.open('');")
time.sleep(5)
# Switch to the new window since the focus would still be on the old window
driver.switch_to.window(driver.window_handles[1])
driver.get("https://lambdatest.com")
time.sleep(5)
# close the active tab
driver.close()
time.sleep(5)
# Switch back to the first tab
driver.switch_to.window(driver.window_handles[0])
driver.get("https://www.yahoo.com")
time.sleep(5)
# Close the only tab, will also close the browser.
#driver.close()

Selenium Tip#13 –处理页面中的下拉菜单

有一个要求,您必须从网页上的下拉菜单中选择一个特定的选项。 您可以通过多种方式从下拉菜单中选择所需的选项。

  • select_by_index(期望的索引值)
  • select_by_visible_text(“ text_to_be_selected_from_drop_down_menu”)
  • select_by_value(值)

我们将根据此要求使用http://demos.dojotoolkit.org/dijit/tests/test_Menu.html进行Selenium自动化测试。 在我们从下拉菜单中选择所需元素之前,获取被测元素的ID非常重要。 我们使用find_element_by_xpath方法来定位该元素,并且一旦找到该元素(使用ID),便从下拉菜单中选择该值。


在下面的示例中,我们显示了可以从菜单中选择元素的不同方法( @ aria-label ='select'

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from time import sleep
from selenium.common.exceptions import NoSuchElementException
from pip._vendor.distlib import resourcesdriver = webdriver.Firefox()
driver.get("http://demos.dojotoolkit.org/dijit/tests/test_Menu.html")''' Let the page load completely '''
sleep(5)try:''' You can derive these details by using Web Inspector '''select_element = Select(driver.find_element_by_xpath("//select[@aria-label='select']"))# Option 1 - Selecting the drop-down item by using the textselect_element.select_by_visible_text("bleed through")sleep(5)# Option 2 - Selecting the drop-down item by using the index valueselect_element.select_by_index(0)sleep(5)# Option 3 - Selection of desired option using value''' This option would fail since there is no value in the pagewhich we are testing right now ''' # select_element.select_by_value('2')
except NoSuchElementException:print("Element not found")''' Addition of delay so that we can have a look at the output '''
sleep(5)''' Release the resources '''
driver.quit()

硒技巧14 –使用复选框处理操作

复选框是网页中的常见元素,用于您仅需从多个选项中选择一个选项的情况下。 像下拉菜单处理一样,我们使用find_element_by_xpath方法找到所需的复选框,一旦找到该复选框,就会执行单击操作。

我们将使用http://demos.dojotoolkit.org/dijit/tests/form/test_CheckBox.html进行Selenium自动化测试,并且要求“选中”值为cb7的复选框:“正常”复选框。 使用driver.find_elements_by_xpath(“ // * [contains(text(),'要搜索的文本')]”)完成匹配。

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from time import sleep
from selenium.common.exceptions import NoSuchElementException
from pip._vendor.distlib import resourcesdriver = webdriver.Firefox()
driver.get("http://demos.dojotoolkit.org/dijit/tests/form/test_CheckBox.html")''' Let the page load completely '''
sleep(20)try:''' You can derive these details by using Web Inspector '''driver.find_element_by_xpath("//*[contains(text(), 'cb7: normal checkbox')]").click()
except NoSuchElementException:print("Element not found")''' Addition of delay so that we can have a look at the output '''
sleep(5)''' Release the resources '''
driver.quit()

Selenium Tip#15 –通过CSS选择器选择元素

在使用Selenium执行测试自动化时,可以使用CSS定位器来定位网页上的元素。 find_elements_by_css_selector可以用于定位必须将要定位的元素详细信息(标签,链接,ID等)作为输入参数传递的元素。 它通过CSS Selector在该元素的子元素中找到元素列表。

目的是使用find_elements_by_css_selector在https://lambdatest.com/上找到“登录”按钮并执行单击操作。 与登录相关的代码如下。 代码检查工具快照还提供了所需的信息。

<html>
........
<li class="login">
<a href="https://accounts.lambdatest.com/register">Free Sign Up</a>
</li>
.....
</html>

因此,我们将li.login作为参数传递给find_element_by_css_selector,一旦找到元素,就执行Click操作。

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from time import sleep
from selenium.common.exceptions import NoSuchElementException
from pip._vendor.distlib import resourcesdriver = webdriver.Firefox()
driver.get("https://www.lambdatest.com/")''' Let the page load completely '''
sleep(20)try:''' You can derive these details by using Web Inspector '''driver.find_element_by_css_selector("li.login").click()
except NoSuchElementException:print("Element not found")''' Addition of delay so that we can have a look at the output '''
sleep(5)''' Release the resources '''
driver.quit()

不要忘记阅读我们有关将CSS选择器用于Selenium进行自动化测试的综合文章。

Selenium Tip#16 –明确等待处理不同的情况

在Selenium自动化测试中观察到一种情况是很正常的,在这种情况下,网页可能需要花费一些时间来加载,或者您希望在触发测试代码之前可以看到页面上的特定Web元素。 在这种情况下,您需要执行“ 显式等待” ,这是一段代码,通过它可以定义要发生的条件,然后再继续执行代码。

Selenium具有WebDriverWait ,可以将其应用于任何具有条件和持续时间的Web元素。 如果不存在执行等待的元素或发生超时,则可能引发异常。

在下面的示例中,我们等待link_text'Sitemap'加载到页面上,并在WebDriverWait方法中指定了超时。 如果在超时时间内未加载该元素,则抛出异常。

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from pip._vendor.distlib import resources
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ECdriver = webdriver.Firefox()
driver.get("https://www.lambdatest.com/")
timeout = 10try:''' We wait till the time link with text SITEMAP is loaded '''''' If that link text is not present on the page, it gives a time out '''''' Try replacing Sitemap with Sitemap123 and you can encounter a timeout '''element_present = EC.presence_of_element_located((By.LINK_TEXT, 'Sitemap'))WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:print("Timed out while waiting for page to load")
driver.quit()
Selenium Tip #17 – Scroll Operations In A Web Page

在使用Selenium执行测试自动化时,您可能需要在页面上执行上滚/下滚操作的要求。 您可以将execute_script与window.scrollTo JS代码用作参数来实现相同的效果。 在下面的示例中,加载被测网站后,我们滚动到页面的末尾。

from selenium import webdriver
from time import sleepdriver = webdriver.Firefox()
driver.get("https://www.lambdatest.com/")
timeout = 10''' Scroll to the end of the page '''
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")''' Sleep is added so that you can have a look at the output '''
sleep(10)''' Scroll again to the top of the page '''
driver.execute_script("window.scroll(0, 0);")sleep(10)
driver.quit()

Selenium Tip#18 –使用Selenium放大和缩小

为了在进行Selenium自动化测试时放大或缩小,应使用transform CSS属性 (适用于相应的浏览器),该属性可让您在页面上执行放大,缩小,旋转,倾斜等操作。

不同类型的浏览器CSS参数如下


在下面的示例中,我们将浏览器中加载的网页缩小200%,然后再放大100%(即恢复正常)。 由于我们使用的是Firefox浏览器,因此我们使用了MozTransform CSS属性。

from selenium import webdriver
from time import sleepdriver = webdriver.Firefox()
driver.get("https://www.lambdatest.com/")
timeout = 10''' Zoom in by 200% '''
driver.execute_script('document.body.style.MozTransform = "scale(2.0)";')
driver.execute_script('document.body.style.MozTransformOrigin = "0 0";')sleep(10)''' Zoom out by 100% '''driver.execute_script('document.body.style.MozTransform = "scale(1.0)";')
driver.execute_script('document.body.style.MozTransformOrigin = "0 0";')''' Sleep is added so that you can have a look at the output '''
sleep(10)''' Release all the resources '''
driver.quit()

硒技巧#19 –在网页中查找元素的大小

您必须首先通过ID搜索元素,然后使用.size属性来计算搜索到的元素的大小。 在下面的示例中,我们在页面http://demos.dojotoolkit.org/dijit/tests/test_Menu.html中计算按钮create_programmatic_menu(ID = createDestoryButton)的大小。


from selenium import webdriver
from time import sleepdriver = webdriver.Firefox()
driver.get("http://demos.dojotoolkit.org/dijit/tests/test_Menu.html")
timeout = 10search_element = driver.find_element_by_id("createDestroyButton")print(search_element.size)''' Release all the resources '''
driver.quit()

当您执行上述代码时,它将输出按钮的大小(ID – CreateDestroyButton)。


Selenium技巧#20 –获取网页中元素的X和Y坐标

您必须遵循用于计算元素大小的类似方法。 您必须首先通过ID搜索元素,然后使用.location属性来计算搜索到的元素的X和Y坐标。

测试URL为http://demos.dojotoolkit.org/dijit/tests/test_Menu.html ,我们计算按钮create_programmatic_menu(ID = createDestoryButton)的X和Y坐标

from selenium import webdriver
from time import sleepdriver = webdriver.Firefox()
driver.get("http://demos.dojotoolkit.org/dijit/tests/test_Menu.html")
timeout = 10search_element = driver.find_element_by_id("createDestroyButton")print(search_element.location)''' Release all the resources '''
driver.quit()

当您执行上述代码时,它将输出按钮(ID – CreateDestroyButton)的X,Y坐标e。


Selenium提示#21 –使用自定义配置文件禁用JavaScript

如果要禁用浏览器JavaScript支持以验证自动跨浏览器与Selenium自动化测试的兼容性,则需要更改被测浏览器的配置文件设置(在本例中为Firefox),并将更改应用于配置文件。 我们使用DEFAULT_PREFERENCES ['frozen'] ['javascript.enabled'] = False禁用浏览器JavaScript支持。

执行代码后,您应该通过在地址栏中输入about:config并搜索javascript.enabled属性的值来验证配置文件的更改。

from selenium import webdriver''' Since we have the geckodriver & Firefox browser in same location '''
''' We do not pass the location of the Firefox profile '''
ff_profile = webdriver.FirefoxProfile()ff_profile.DEFAULT_PREFERENCES['frozen']['javascript.enabled'] = False
ff_profile.set_preference("app.update.auto", False)
ff_profile.set_preference("app.update.enabled", False)''' Update the preferences '''
ff_profile.update_preferences()''' Load the Firefox browser with the updated profile '''
driver = webdriver.Firefox(ff_profile)''' Verify whether the changes are working fine or not '''
driver.get("about:config")

下面是Firefox中about:config设置的屏幕截图(代码执行后)


Selenium提示#22 –设置手动代理设置

在某些情况下,您可能需要更改代理设置才能执行测试。 要更改代理设置,需要首先导入模块selenium.webdriver.common.proxy 。 您必须将代理类型设置为MANUAL ,然后更改代理设置,然后将新设置应用到被测浏览器(在我们的示例中为Firefox)。

您需要用计划用于测试的IP地址和端口号替换ip_address和port_number。

from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyTypeproxy_settings = Proxy()''' The proxy settings are first changed to Manual '''
proxy_settings.proxy_type = ProxyType.MANUAL''' Replace ip_address with Proxy IP address and '''
''' port_number with the port number which you plan to use '''
proxy_settings.http_proxy = "ip_address:port_number"
proxy_settings.socks_proxy = "ip_address:port_number"
proxy_settings.ssl_proxy = "ip_address:port_number"''' You can add capabilties for different browsers & devices '''
capabilities = webdriver.DesiredCapabilities.FIREFOX
proxy_settings.add_to_capabilities(capabilities)driver = webdriver.Firefox(desired_capabilities=capabilities)

结论

我们涵盖了大多数Selenium技巧,可帮助您像专业人员一样使用Selenium进行测试自动化。 尽管您可以使用本地计算机在不同的浏览器,设备和操作系统上验证您的网站/网络应用程序,但是由于您无法涵盖设备+操作系统+浏览器(及浏览器版本)的全部范围,因此测试的范围将受到限制。 在这里,您应该利用这些提示和技巧对网站/ Web应用程序执行自动跨浏览器测试。

当您开始通过在超过2000种真实的浏览器+ OS +设备组合上测试Web应用程序来扩展测试范围时,必须进行最小的更改才能将本地Selenium自动化测试代码移至LambdaTest 云上Selenium Grid 。 您还可以集成到许多CI CD工具,项目管理工具等。

使用Selenium进行测试自动化可能是一项艰巨的任务,但是要获得可行的见解,我在本文上面列出了一些有关Selenium自动化测试的实用技巧。 您可以精通Selenium中的测试自动化。 让我知道是否还有其他有关使用Selenium进行测试自动化的技巧可以帮助您快速跟踪测试周期。 干杯和快乐的测试!

翻译自: https://www.javacodegeeks.com/2019/07/test-automation-selenium-webdriver.html

使用Selenium WebDriver测试自动化的22条实用技巧相关推荐

  1. 自动化比手工测试成本高?使用Selenium评估测试自动化的ROI指标

    跨浏览器测试是一种测试,需要大量的精力和时间.通过不同的浏览器,操作系统,设备,屏幕分辨率测试Web应用程序,以评估针对各种受众的Web内容呈现的过程是一项活动. 特别是如果手动处理,使用Seleni ...

  2. Selenium WebDriver 测试Chrome浏览器

    文章目录 (1)安装驱动 (2)代码测试 (3)注意事项 (1)安装驱动 Selenium WebDriver如果需要启动Chrome浏览器,需要下载对应的chromedriver驱动器. [下载地址 ...

  3. 1.搭建Java+Selenium+WebDriver测试环境

    1.浏览器:Firefox  40.0 ,在浏览器 的附件组件 搜索并安装插件 Firebug .FirePath 辅助测试. Firebug 可以用来查看页面元素对应的HTML代码. FirePat ...

  4. 22条日常技巧助程序员提高工作效率、节约时间

    这不是一个关于时间的问题,而是关于精力. 为了"有效率",我们试图在一个工作日内塞进尽可能多的小时,但最终一切都更多地取决于你的注意力,积极性和良好状态(这些都是直接与精力等级联系 ...

  5. Selenium WebDriver——如何测试REST API

    文章首发于微信公众号[软测小生] 前言: 关于如何使用selenium webdriver测试REST api的问题,你可以在StackOverflow.com上看到很多相关的问题.不熟悉自动化测试的 ...

  6. selenium webdriver模拟鼠标键盘操作

    在测试使用Selenium webdriver测试WEB系统的时候,用到了模拟鼠标.键盘的一些输入操作. 1.鼠标的左键点击.双击.拖拽.右键点击等: 2.键盘的回车.回退.空格.ctrl.alt.s ...

  7. Selenium 自动测试软件的使用(自动化操作)

    Selenium是一个用于Web应用程序测试的工具,很多时候可以拿来做自动化的功能,解放一些重复的事情,比如自动注册.登录.获取信息.处理报表等等,都是可以自己去设计一个流程即可,使用它,先从安装和配 ...

  8. 「UI 测试自动化selenium」汇总

    <selenium 基础之java实现> selenium RC 环境配置 菜鸟学自动化测试(一)----selenium IDE 菜鸟学自动化测试(二)----selenium IDE ...

  9. selenium自动化测试_Selenium测试可实现有效的测试自动化

    selenium自动化测试 尽管移动应用程序的市场份额有大幅增长,但网络应用程序仍然具有大量的用户基础. 在部署之前,企业正在关注Web应用程序的质量. 这是测试发挥重要作用的地方. 与某些可以自动化 ...

最新文章

  1. XMPP通讯开发-好友获取界面设计
  2. c#/.net 循序渐进理解-委托
  3. 【河南省多校脸萌第六场 E】LLM找对象
  4. 关于CRM WebClient UI缓存清理的讨论
  5. 【C语言进阶深度学习记录】三十九 C语言中的可变参数(参数可变的函数)
  6. leetcode263. 丑数
  7. 主元排序法c语言程序,C语言算法竞赛入门(二)---数组元素移动 、排序问题 、猴子选大王问题...
  8. 利用next_permutation解答全排列问题
  9. 计算机教师自媒体方向,教师和自媒体,我该选择哪个深耕?
  10. 开启服务_Nike Dunk 开启定制服务
  11. Android用按钮确定单选框,Android RadioButton单选框的使用方法
  12. CentOS 7以yum方式安装zabbix3.2及配置文件详解
  13. 搭建一个简单Git服务器
  14. bzoj3714 [PA2014]Kuglarz
  15. Cadence Allegro Segments Over Voids 功能介绍图文教程
  16. 基于大数据的一线城市住房租赁影响因素分析
  17. 《计算机文件管理》教学设计,《文件和文件夹的操作》教学设计
  18. 计算机的操作系统的功能有哪些,操作系统的主要功能是主要有哪些类型
  19. Windows分盘,c盘已经满了,但d盘还有内存。
  20. 高德地图使用鼠标工具(mouseTool)画覆盖物折线(mouseTool.polyline),光标使用十字架(crosshair)类型,不断出现closehand小手图标干扰

热门文章

  1. jzoj3910-Idiot的间谍网络【倍增,dfs】
  2. jzoj3058-火炬手【高精度,暴力】
  3. P2153-晨跑【费用流,网络流,拆点】
  4. 牛客练习赛 58——树链剖分
  5. 【数论】疯狂 LCM(P1891)
  6. 【堆】【DP】Niyaz and Small Degrees(luogu 7600[APIO 2021 T3]/luogu-CF1119F)
  7. 洛谷 P1967货车运输 并查集+贪心 不需要用LCA!
  8. 常用公有云接入——腾讯
  9. 你必须掌握的 21 个 Java 核心技术
  10. 深入JVM虚拟机(四) Java GC收集器