Python - 使用Selenium打开多个选项卡
测试是软件方法论中的一个重要概念。据说软件只有在没有错误的情况下才是有效和高效的。测试可以手动完成,也可以通过自动化完成。在Python, selenium用于进行自动化测试。 selenium包可用,它们对于从Python自动化 Web 浏览器交互非常有帮助。
在本文中,我们将讨论如何使用selenium打开多个选项卡。
安装
pip install selenium
不同浏览器的Selenium驱动程序要求:
每个浏览器都是不同的,同样,它们需要不同的selenium webdriver。
Chrome、Firefox等流行浏览器,其webdriver下载路径如下
Chrome: https://sites.google.com/a/chromium.org/chromedriver/downloads
Edge: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
Firefox: https://github.com/mozilla/geckodriver/releases
Safari: https://webkit.org/blog/6900/webdriver-support-in-safari-10
首先,让我们看一个打开firefox浏览器的正常例子
所需步骤:
- 我们需要 Geckodriver 来打开 Firefox 浏览器。可以从 https://github.com/mozilla/geckodriver/releases 下载。它必须在 Windows 中的“Path”变量中设置,在 Linux 和 Mac 中也必须在相应的位置进行设置。
- 从Python打开一个新的 Firefox 浏览器。
- 然后在给定的有效 URL 加载页面。
Python3
# Necessary webdrivers ned to be imported
from selenium import webdriver
# This is for Firefox. Similarly if
# chrome is needed , then it has to be specified
webBrowser = webdriver.Firefox()
# This will open geeksforgeeks site in Firefox
webBrowser.get('https://www.geeksforgeeks.org/')
Python3
# Necessary webdrivers ned to be imported
from selenium import webdriver
# This is for Firefox. Similarly if
# chrome is needed , then it has to be specified
webBrowser = webdriver.Firefox()
# first tab. Open google.com in the first tab
webBrowser.get('http://google.com')
# second tab
# execute_script->Executes JavaScript snippet.
# Here the snippet is window.open that means, it
# opens in a new browser tab
webBrowser.execute_script("window.open('about:blank',
'secondtab');")
# It is switching to second tab now
webBrowser.switch_to.window("secondtab")
# In the second tab, it opens geeksforgeeks
webBrowser.get('https://www.geeksforgeeks.org/')
Python3
# Necessary webdrivers ned to be imported
from selenium import webdriver
# Get the instance of the webBrowser
# window, here we are using Chrome
webBrowser = webdriver.Chrome()
# Lets open google.com in the first tab
webBrowser.get('http://google.com')
# Lets open https://www.bing.com/ in the second tab
webBrowser.execute_script("window.open('about:blank',
'secondtab');")
webBrowser.switch_to.window("secondtab")
webBrowser.get('https://www.bing.com/')
# Lets open https://www.facebook.com/ in the third tab
webBrowser.execute_script("window.open('about:blank',
'thirdtab');")
webBrowser.switch_to.window("thirdtab")
webBrowser.get('https://www.facebook.com/')
Python3
# Necessary imports
from selenium import webdriver
# initially webdriver is empty
webdriver.driver = None
browserName = input("Enter your browser name(chrome/firefox/edge/ie):")
# Depends upon the browser name, drivers are selected,
# in order to check for all given 4 browser checkings,
# all 4 drivers must be installed and they should be
# available in "Path"
if browserName.upper() == "CHROME":
driver = webdriver.Chrome()
elif browserName.upper() == "FIREFOX":
driver = webdriver.Firefox()
elif browserName.upper() == "EDGE":
# MicrosoftWebDriver.exe should be
# downloaded and available in Path
driver = webdriver.Edge()
elif browserName.upper() == "IE":
# IEDriverServer.exe should be
# downloaded and available in Path
driver = webdriver.Ie()
else:
print("No browser is specified")
# Lets open google.com in the first tab
driver.get('http://google.com')
# Lets open https://www.bing.com/ in the second tab
driver.execute_script("window.open('about:blank',
'secondtab');")
driver.switch_to.window("secondtab")
driver.get('https://www.bing.com/')
# Lets open https://www.facebook.com/ in the third tab
driver.execute_script("window.open('about:blank',
'thirdtab');")
driver.switch_to.window("thirdtab")
driver.get('https://www.facebook.com/')
# It is always good to quit the driver
# driver.quit()
在执行代码时,我们可以看到如下所示的操作:
使用Selenium打开多个选项卡的方法:
- 为selenium指定 Firefox/Chrome 驱动程序后,首先,我们需要打开一个网页
- 我们需要调用“execute_script”方法,该方法依次执行 window.open('about:blank', 'secondtab') javascript。
- 然后我们需要切换到该选项卡,该选项卡可以提供任何有效的 URL。
蟒蛇3
# Necessary webdrivers ned to be imported
from selenium import webdriver
# This is for Firefox. Similarly if
# chrome is needed , then it has to be specified
webBrowser = webdriver.Firefox()
# first tab. Open google.com in the first tab
webBrowser.get('http://google.com')
# second tab
# execute_script->Executes JavaScript snippet.
# Here the snippet is window.open that means, it
# opens in a new browser tab
webBrowser.execute_script("window.open('about:blank',
'secondtab');")
# It is switching to second tab now
webBrowser.switch_to.window("secondtab")
# In the second tab, it opens geeksforgeeks
webBrowser.get('https://www.geeksforgeeks.org/')
输出:
也可以使用 chrome 驱动程序运行上述相同的程序。 Chrome 驱动程序是特定于版本的,因此与您的 chrome 浏览器的版本不同,我们需要下载
上面代码中的一个小变化是代替“webdriver.Firefox()”,我们应该有 webdriver.Chrome()
现在让我们看看如何使用 chrome 驱动程序打开 3 个标签页
蟒蛇3
# Necessary webdrivers ned to be imported
from selenium import webdriver
# Get the instance of the webBrowser
# window, here we are using Chrome
webBrowser = webdriver.Chrome()
# Lets open google.com in the first tab
webBrowser.get('http://google.com')
# Lets open https://www.bing.com/ in the second tab
webBrowser.execute_script("window.open('about:blank',
'secondtab');")
webBrowser.switch_to.window("secondtab")
webBrowser.get('https://www.bing.com/')
# Lets open https://www.facebook.com/ in the third tab
webBrowser.execute_script("window.open('about:blank',
'thirdtab');")
webBrowser.switch_to.window("thirdtab")
webBrowser.get('https://www.facebook.com/')
在执行脚本时,我们可以看到:
让我们检查如何通过将浏览器名称作为输入并打开多个选项卡来指定不同的驱动程序
蟒蛇3
# Necessary imports
from selenium import webdriver
# initially webdriver is empty
webdriver.driver = None
browserName = input("Enter your browser name(chrome/firefox/edge/ie):")
# Depends upon the browser name, drivers are selected,
# in order to check for all given 4 browser checkings,
# all 4 drivers must be installed and they should be
# available in "Path"
if browserName.upper() == "CHROME":
driver = webdriver.Chrome()
elif browserName.upper() == "FIREFOX":
driver = webdriver.Firefox()
elif browserName.upper() == "EDGE":
# MicrosoftWebDriver.exe should be
# downloaded and available in Path
driver = webdriver.Edge()
elif browserName.upper() == "IE":
# IEDriverServer.exe should be
# downloaded and available in Path
driver = webdriver.Ie()
else:
print("No browser is specified")
# Lets open google.com in the first tab
driver.get('http://google.com')
# Lets open https://www.bing.com/ in the second tab
driver.execute_script("window.open('about:blank',
'secondtab');")
driver.switch_to.window("secondtab")
driver.get('https://www.bing.com/')
# Lets open https://www.facebook.com/ in the third tab
driver.execute_script("window.open('about:blank',
'thirdtab');")
driver.switch_to.window("thirdtab")
driver.get('https://www.facebook.com/')
# It is always good to quit the driver
# driver.quit()
Selenium测试正在所有软件行业中得到应用。它更快更有效。通过selenium测试也克服了手动错误。随着自动化无处不在,测试也只有在今天才能以自动化方式完成。打开多个选项卡以检查不同的功能是一项常见任务。