如何使用浏览器版本?
>>> from selenium import webdriver
>>> driver = webdriver.Firefox()
>>> print version <-- how to do this?
Firefox 12.0
答案 0 :(得分:39)
这个答案让我走上正确的道路但是特定于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);
答案 1 :(得分:3)
虽然这可能不能完全回答上述问题,但对于那些根据从不同浏览器(即Firefox与Chrome)收到的不同行为寻找测试代码的方法,这仍然有用。当我偶然发现这个帖子时,我正在寻找这个,所以我想我会添加它,以防它可以帮助别人。
在Python上,如果你只是在寻找你正在测试的浏览器(即firefox,chrome,ie等),那么你可以使用......
driver.name
...在 if 语句中。这假设您已经将驱动程序分配给您正在测试的Web浏览器(即Firefox,Chrome,IE等)。但是,如果您的任务是测试同一浏览器的多个版本,那么您需要更多的内容 driver.version 。希望这有助于某人。当我找到这个帖子时,我正在寻找这个解决方案,所以我想我会添加它以防其他人需要它。
答案 2 :(得分:2)
如果您将WebDriver打包成EventFiring,则必须执行自定义的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();
}
}
刚发布,因为这是我遇到的问题。
答案 3 :(得分:2)
只需为想要在我不知道的情况下打印所有功能的Python用户回答此问题。下面的命令有效。
print driver.capabilities
答案 4 :(得分:2)
如果您使用的是Chrome,则可以执行以下操作:
driver.capabilities['version']
如果您使用的是Firefox:
driver.capabilities['browserVersion']
答案 5 :(得分:1)
如果allOf
对您不起作用,请检查功能。有版本号,但可能在其他密钥下。例如,当我尝试使用MyModel
键访问版本号时,在Windows 10上出现键错误。
要检查功能:
driver.capabilities['version']
对我来说,这适用于Chrome / Linux
version
这在Chrome / Windows 10上有效
print driver.capabilities
答案 6 :(得分:0)
您可以通过访问返回GeckoDriver的firefox对象来提取capabilities发起的dictionary会话的浏览器版本,您可以使用以下解决方案:
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
同样,您可以按以下步骤从字典中提取所有属性:
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()
控制台输出:
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