当我在Selenium WebDriver中获得页面时,幕后会发生什么

时间:2016-10-06 00:34:40

标签: selenium selenium-webdriver webdriver

假设我有一段代码表明:

from selenium import webdriver

driver = webdriver.Firefox()

driver.get('http://stackoverflow.com/')

运行最后一个命令时webdriver和网页会发生什么?我的理论来了:

  • 东西(是网络驱动程序还是我的电脑或者其他东西 else)向服务器发送GET请求。
  • HTML从响应中获取并加载注入 webdriver(或者如果请求是从 的webdriver)

如果我的假设看似愚蠢,请原谅。

1 个答案:

答案 0 :(得分:1)

Webdriver是W3C发布的规范。您可以查看here

每个浏览器“制造商”都有责任实施规范,因此浏览器可以支持它。 Selenium提供了一个API,可以更轻松地使用它(link)。

以下是Java的Selenium源代码,其中包含一些有用的javadoc - WebDriver.get(url)方法。

/**
* Load a new web page in the current browser window. This is done using an HTTP GET operation,
* and the method will block until the load is complete. This will follow redirects issued either
* by the server or as a meta-redirect from within the returned HTML. Should a meta-redirect
* "rest" for any duration of time, it is best to wait until this timeout is over, since should
* the underlying page change whilst your test is executing the results of future calls against
* this interface will be against the freshly loaded page. Synonym for
* {@link org.openqa.selenium.WebDriver.Navigation#to(String)}.
*
* @param url The URL to load. It is best to use a fully qualified URL
*/
void get(String url);

get(String url)RemoteWebDriver.java代码的实施是:

public void get(String url) {
    execute(DriverCommand.GET, ImmutableMap.of("url", url));
}

如果想深入了解,可以从this address克隆来自Github的回购,并检查班级protected Response execute(String driverCommand, Map<String, ?> parameters)RemoteWebDriver.java的整个实施。 它将更详细地向您展示在请求页面时会发生什么。