使用されているブラウザのバージョンを取得するにはどうすればよいですか?
>>> from selenium import webdriver
>>> driver = webdriver.Firefox()
>>> print version <-- how to do this?
Firefox 12.0
使用されているブラウザのバージョンを取得するにはどうすればよいですか?
>>> from selenium import webdriver
>>> driver = webdriver.Firefox()
>>> print version <-- how to do this?
Firefox 12.0
この答えは私を正しい道に導きましたが、Pythonに固有であり、トピックはより広範です。だから、私はもう少しトリッキーだったJavaの答えを追加しています。現在、私はセレン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);
プロパティは、capabilities
ブラウザー自体に関する情報を含む辞書であるため、これは機能するはずです。
print(driver.capabilities['version'])
これは上記の質問に対する完全な回答ではないかもしれませんが、さまざまなブラウザー (Firefox と Chrome など) から受け取ったさまざまな動作に基づいてテストをコーディングする方法を探している人にとっては、それでも役立つ可能性があります。このスレッドに出くわしたときにこれを探していたので、他の人の助けになる場合に備えて追加すると思いました.
Python では、テストしているブラウザ (Firefox、Chrome など) を単に探している場合は、次のように使用できます...
driver.name
... ifステートメントで。これは、テストしている Web ブラウザー (つまり、Firefox、Chrome、IE など) に既にドライバーを割り当てていることを前提としています。ただし、同じブラウザーの複数のバージョンをテストする必要がある場合は、driver.versionにさらに何かが必要になります。これが誰かを助けることを願っています。このスレッドを見つけたときにこの解決策を探していたので、他の誰かが必要とする場合に備えて追加すると思いました.
ディクショナリを返す機能オブジェクトにアクセスすることで、GeckoDriverが開始したFirefoxセッションのブラウザ バージョンを抽出できます。次のソリューションを使用できます。
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