3. Navigating
The first thing you’ll want to do with WebDriver is navigate to a link. The normal way to do this is by calling get method:
driver.get("http://www.google.com")
WebDriver will wait until the page has fully loaded (that is, the onload event has fired) before returning control to your test or script. Be aware that if your page uses a lot of AJAX on load then WebDriver may not know when it has completely loaded. If you need to ensure such pages are fully loaded then you can use waits.
3.1. Interacting with the page
Just being able to go to places isn’t terribly useful. What we’d really like to do is to interact with the pages, or, more specifically, the HTML elements within a page. First of all, we need to find one. WebDriver offers a number of ways to find elements. For example, given an element defined as:
<input type="text" name="passwd" id="passwd-id" />
you could find it using any of:
element = driver.find_element_by_id("passwd-id")
element = driver.find_element_by_name("passwd")
element = driver.find_element_by_xpath("//input[@id='passwd-id']")
element = driver.find_element_by_css_selector("input#passwd-id")
本文介绍如何使用WebDriver进行页面导航及与HTML元素交互的方法。通过调用get方法可以打开指定网页,WebDriver会等待页面完全加载。此外,还介绍了多种查找页面元素的方式,包括按ID、名称、XPath和CSS选择器等。
733

被折叠的 条评论
为什么被折叠?



