Browser interactions

Get browser information

Get title

You can read the current page title from the browser:

Move Code

/examples/java/src/test/java/dev/selenium/interactions/InteractionsTest.java
packagedev.selenium.interactions;importdev.selenium.BaseChromeTest;importorg.junit.jupiter.api.Assertions;importorg.junit.jupiter.api.Test;publicclass InteractionsTestextendsBaseChromeTest{@TestpublicvoidgetTitle(){driver.get("https://www.selenium.dev/");// get titleStringtitle=driver.getTitle();Assertions.assertEquals(title,"Selenium");}@TestpublicvoidgetCurrentUrl(){driver.get("https://www.selenium.dev/");// get current urlStringurl=driver.getCurrentUrl();Assertions.assertEquals(url,"https://www.selenium.dev/");}}
title = driver.title
/examples/python/tests/interactions/test_interactions.py
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.selenium.dev")
title = driver.title
assert title == "Selenium"
url = driver.current_url
assert url == "https://www.selenium.dev/"
driver.quit()
 String title = driver.Title;
/examples/dotnet/SeleniumDocs/Interactions/InteractionsTest.cs
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace SeleniumDocumentation.SeleniumInteractions
{
 [TestClass]
 public class InteractionsTest
 {
 [TestMethod]
 public void TestInteractions()
 {
 WebDriver driver = new ChromeDriver();
 driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(500);
 // Navigate to Url
 driver.Url="https://www.selenium.dev/";
 //GetTitle
 String title = driver.Title;
 Assert.AreEqual(title, "Selenium");
 //GetCurrentURL
 String url = driver.Url;
 Assert.AreEqual(url, "https://www.selenium.dev/");
 //quitting driver
 driver.Quit(); //close all windows
 }
 }
}
 it 'gets the current title' do
/examples/ruby/spec/interactions/browser_spec.rb
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Browser' do
 let(:driver) { start_session }
 it 'gets the current title' do
 driver.navigate.to 'https://www.selenium.dev/'
 current_title = driver.title
 expect(current_title).to eq 'Selenium'
 end
 it 'gets the current url' do
 driver.navigate.to 'https://www.selenium.dev/'
 current_url = driver.current_url
 expect(current_url).to eq 'https://www.selenium.dev/'
 end
end
 let title = await driver.getTitle();
/examples/javascript/test/interactions/interactionsIndex.spec.js
const {Builder } = require('selenium-webdriver');
const assert = require("node:assert");
describe('Interactions', function () {
 let driver;
 before(async function () {
 driver = new Builder()
 .forBrowser('chrome')
 .build();
 });
 after(async () => await driver.quit());
 it('Should be able to get title and current url', async function () {
 const url = 'https://www.selenium.dev/';
 await driver.get(url);
 //Get Current title
 let title = await driver.getTitle();
 assert.equal(title, "Selenium");
 //Get Current url
 let currentUrl = await driver.getCurrentUrl();
 assert.equal(currentUrl, url);
 });
});
driver.title

Get current URL

You can read the current URL from the browser’s address bar using:

Move Code

/examples/java/src/test/java/dev/selenium/interactions/InteractionsTest.java
packagedev.selenium.interactions;importdev.selenium.BaseChromeTest;importorg.junit.jupiter.api.Assertions;importorg.junit.jupiter.api.Test;publicclass InteractionsTestextendsBaseChromeTest{@TestpublicvoidgetTitle(){driver.get("https://www.selenium.dev/");// get titleStringtitle=driver.getTitle();Assertions.assertEquals(title,"Selenium");}@TestpublicvoidgetCurrentUrl(){driver.get("https://www.selenium.dev/");// get current urlStringurl=driver.getCurrentUrl();Assertions.assertEquals(url,"https://www.selenium.dev/");}}
url = driver.current_url
/examples/python/tests/interactions/test_interactions.py
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.selenium.dev")
title = driver.title
assert title == "Selenium"
url = driver.current_url
assert url == "https://www.selenium.dev/"
driver.quit()
 String url = driver.Url;
/examples/dotnet/SeleniumDocs/Interactions/InteractionsTest.cs
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace SeleniumDocumentation.SeleniumInteractions
{
 [TestClass]
 public class InteractionsTest
 {
 [TestMethod]
 public void TestInteractions()
 {
 WebDriver driver = new ChromeDriver();
 driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(500);
 // Navigate to Url
 driver.Url="https://www.selenium.dev/";
 //GetTitle
 String title = driver.Title;
 Assert.AreEqual(title, "Selenium");
 //GetCurrentURL
 String url = driver.Url;
 Assert.AreEqual(url, "https://www.selenium.dev/");
 //quitting driver
 driver.Quit(); //close all windows
 }
 }
}
 it 'gets the current url' do
/examples/ruby/spec/interactions/browser_spec.rb
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Browser' do
 let(:driver) { start_session }
 it 'gets the current title' do
 driver.navigate.to 'https://www.selenium.dev/'
 current_title = driver.title
 expect(current_title).to eq 'Selenium'
 end
 it 'gets the current url' do
 driver.navigate.to 'https://www.selenium.dev/'
 current_url = driver.current_url
 expect(current_url).to eq 'https://www.selenium.dev/'
 end
end
 let currentUrl = await driver.getCurrentUrl();
/examples/javascript/test/interactions/interactionsIndex.spec.js
const {Builder } = require('selenium-webdriver');
const assert = require("node:assert");
describe('Interactions', function () {
 let driver;
 before(async function () {
 driver = new Builder()
 .forBrowser('chrome')
 .build();
 });
 after(async () => await driver.quit());
 it('Should be able to get title and current url', async function () {
 const url = 'https://www.selenium.dev/';
 await driver.get(url);
 //Get Current title
 let title = await driver.getTitle();
 assert.equal(title, "Selenium");
 //Get Current url
 let currentUrl = await driver.getCurrentUrl();
 assert.equal(currentUrl, url);
 });
});
driver.currentUrl