浏览器交互

获取浏览器信息

获取标题

从浏览器中读取当前页面的标题:

      String title = driver.getTitle();
Show full example
package dev.selenium.interactions;

import dev.selenium.BaseChromeTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Cookie;
import java.util.Set;

public class InteractionsTest extends BaseChromeTest {
  @Test
  public void getTitle() {
    try {
      driver.get("https://www.selenium.dev/");
      // get title
      String title = driver.getTitle();
      Assertions.assertEquals(title, "Selenium");
    } finally {
      driver.quit();
    }
  }
  @Test
  public void getCurrentUrl() {
    try {
      driver.get("https://www.selenium.dev/");
      // get current url
      String url = driver.getCurrentUrl();
      Assertions.assertEquals(url, "https://www.selenium.dev/");
    } finally {
      driver.quit();
    }
  }
}
title = driver.title
Show full example
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;
Show full example
// 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
        }
    }
}
    current_title = driver.title
Show full example
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();
Show full example
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

获取当前 URL

您可以从浏览器的地址栏读取当前的 URL,使用:

      String url = driver.getCurrentUrl();
Show full example
package dev.selenium.interactions;

import dev.selenium.BaseChromeTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Cookie;
import java.util.Set;

public class InteractionsTest extends BaseChromeTest {
  @Test
  public void getTitle() {
    try {
      driver.get("https://www.selenium.dev/");
      // get title
      String title = driver.getTitle();
      Assertions.assertEquals(title, "Selenium");
    } finally {
      driver.quit();
    }
  }
  @Test
  public void getCurrentUrl() {
    try {
      driver.get("https://www.selenium.dev/");
      // get current url
      String url = driver.getCurrentUrl();
      Assertions.assertEquals(url, "https://www.selenium.dev/");
    } finally {
      driver.quit();
    }
  }
}
url = driver.current_url
Show full example
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;
Show full example
// 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
        }
    }
}
    current_url = driver.current_url
Show full example
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();
Show full example
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