Python 使用SeleniumWebDriver获取浏览器版本
如何使用浏览器版本Python 使用SeleniumWebDriver获取浏览器版本,python,selenium,selenium-webdriver,firefox,geckodriver,dictionary,Python,Selenium,Selenium Webdriver,Firefox,Geckodriver,Dictionary,如何使用浏览器版本 >>> from selenium import webdriver >>> driver = webdriver.Firefox() >>> print version <-- how to do this? Firefox 12.0 来自selenium导入webdriver的> >>>driver=webdriver.Firefox() >>>打印版本该属性是一个包含有关浏览器本身信息的字典,因此应该
>>> from selenium import webdriver
>>> driver = webdriver.Firefox()
>>> print version <-- how to do this?
Firefox 12.0
来自selenium导入webdriver的>
>>>driver=webdriver.Firefox()
>>>打印版本该属性是一个包含有关浏览器本身信息的字典,因此应该可以:
print(driver.capabilities['version'])
这个答案引导我走上了正确的道路,但它是针对python的,而且主题更广泛。所以,我为Java添加了一个更棘手的答案。现在我正在使用selenium 2.25.0
//make sure have correct import statements - I had to add these
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
WebDriver driver = new FirefoxDriver();
Capabilities caps = ((RemoteWebDriver) driver).getCapabilities();
String browserName = caps.getBrowserName();
String browserVersion = caps.getVersion();
System.out.println(browserName+" "+browserVersion);
如果您将WebDriver包装为EventFireing,则必须执行自定义EventFiringWebDriver实现
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.HasCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
public class MyEventFiringWebDriver extends EventFiringWebDriver implements HasCapabilities {
private RemoteWebDriver driver;
public MyEventFiringWebDriver(RemoteWebDriver driver) {
super(driver);
this.driver = driver;
}
@Override
public Capabilities getCapabilities() {
return driver.getCapabilities();
}
}
只是因为这是我遇到的一个问题而发帖。虽然这可能并不能完全回答上述问题,但对于那些正在寻找基于不同浏览器(即Firefox与Chrome)的不同行为编写测试代码的人来说,这仍然很有用。当我偶然发现这条线索时,我正在寻找它,所以我想我应该添加它,以防它能帮助其他人
在Python上,如果您只是在寻找正在测试的浏览器(如firefox、chrome、ie等),那么您可以使用
driver.name
。。。在if语句中。这假设您已经为正在测试的web浏览器(即Firefox、Chrome、IE等)分配了驱动程序。但是,如果您的任务是测试同一浏览器的多个版本,那么您将需要对driver.version进行更多的测试。希望这能帮助别人。当我找到这个线程时,我正在寻找这个解决方案,所以我想我应该添加它,以防其他人需要它。只是为那些想打印所有功能的Python用户回答这个问题,因为我在不知不觉中搜索它。下面的命令起作用
打印驱动程序。功能
如果您使用的是Chrome,则可以执行以下操作:
driver.capabilities['version']
如果您使用的是Firefox:
driver.capabilities['browserVersion']
如果driver.capabilities['version']
不适合您,请检查这些功能。版本号在那里,但可能在不同的键下。例如,当我尝试使用version
键访问版本号时,在Windows 10上遇到一个键错误
要检查功能,请执行以下操作:
print driver.capabilities
对我来说,这在Chrome/Linux上有效
driver.capabilities['version']
这适用于Chrome/Windows10
driver.capabilities['browserVersion']
您可以通过访问返回a的对象来提取启动会话的浏览器版本,您可以使用以下解决方案:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'
driver = webdriver.Firefox(firefox_options=options, executable_path=r'C:\WebDrivers\geckodriver.exe')
my_dict = driver.capabilities
print("Mozilla Firefox browser version is: " + str(my_dict['browserVersion']))
driver.quit()
控制台输出:
Mozilla Firefox browser version is: 77.0.1
acceptInsecureCerts => True
browserName => firefox
browserVersion => 77.0.1
moz:accessibilityChecks => False
moz:buildID => 20200602222727
moz:geckodriverVersion => 0.26.0
moz:headless => False
moz:processID => 12668
moz:profile => C:\Users\Soma Bhattacharjee\AppData\Local\Temp\rust_mozprofileFc1B08
moz:shutdownTimeout => 60000
moz:useNonSpecCompliantPointerOrigin => False
moz:webdriverClick => True
pageLoadStrategy => normal
platformName => windows
platformVersion => 10.0
rotatable => False
setWindowRect => True
strictFileInteractability => False
timeouts => {'implicit': 0, 'pageLoad': 300000, 'script': 30000}
unhandledPromptBehavior => dismiss and notify
提取所有功能
同样,您可以从字典中提取所有属性,如下所示:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'
driver = webdriver.Firefox(firefox_options=options, executable_path=r'C:\WebDrivers\geckodriver.exe')
my_dict = driver.capabilities
for key,val in my_dict.items():
print (key, "=>", val)
driver.quit()
控制台输出:
Mozilla Firefox browser version is: 77.0.1
acceptInsecureCerts => True
browserName => firefox
browserVersion => 77.0.1
moz:accessibilityChecks => False
moz:buildID => 20200602222727
moz:geckodriverVersion => 0.26.0
moz:headless => False
moz:processID => 12668
moz:profile => C:\Users\Soma Bhattacharjee\AppData\Local\Temp\rust_mozprofileFc1B08
moz:shutdownTimeout => 60000
moz:useNonSpecCompliantPointerOrigin => False
moz:webdriverClick => True
pageLoadStrategy => normal
platformName => windows
platformVersion => 10.0
rotatable => False
setWindowRect => True
strictFileInteractability => False
timeouts => {'implicit': 0, 'pageLoad': 300000, 'script': 30000}
unhandledPromptBehavior => dismiss and notify
从Firefox版本48和更高版本中,您可以看到答案以及由@ArgiesDariodriver链接的答案。功能[“browserVersion”]
Chrome,FF至少现在都实现了该功能。