[Python3 爬虫学习笔记]Selenium使用详解 1 -- 打开一个页面

使用WebDriver打开一个链接,常规的方法是调用get方法:

driver.get("http://www.google.com")

WebDriver将等待,直到页面完全加载完毕(其实是等到onload方法执行完毕),然后返回继续执行脚本。值得注意的是,如果打开的页面使用了大量的Ajax加载,WebDriver可能不知道什么时候页面已经完全加载。这个时候,需要使用wait。

与页面交互

只打开页面是没有什么意义的,我们真正想要的是与页面做到交互。更具体地说,对于一个页面中的HTML,首先我们要找到需要的元素。WebDriver提供了大量的方法帮助我们查找元素,例如:已知一个元素定义如下:

<input type="text" name="passwd" id="passwd-id" />

我们可以通过下面的方法查找到:

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']")

我们还可以通过链接的文本查找到元素,需要注意的是,这个文本必须完全的匹配。当我们使用’XPATH’时,必须注意,如果匹配超过一个元素,只fanti第一个元素。如果没有找到元素,将会抛出’NoSuchElement’异常。
WebDriver有一个’基于对象’的API,我们使用相同的接口表示所有类型的元素。这就意味着,当你打开你的IDE的自动不全功能的时候,你会有很多可以调用的方法。但是并不是所有的方法都是有意义或者有效的。当你调用一些毫无意义的方法是,WebDriver会尝试去做一些正确的事情。
所以,当你拿到一个元素时,你可能会想在文本框中输入一些内容:

element.send_keys("some text")

你还可以通过"Keys"类来模拟输入方向键:

element.send_keys("and some", Keys.ARROW_DOWN)

对于任何元素,都可以调用send_keys方法,这使得它可以测试键盘快捷键,比如当你使用Gmail的时候。但是有一个缺点就是当你输入一些文本时,这些输入框中原有的文本不会被自动清除掉,相反,你的输入会继续添加到已存在的文本之后。你可以很方便的使用clear方法去清除input或者textarea元素中的内容:

element.clear()

填写表格

我们已经直到如何在input或textarea元素输入内容,但是其他元素怎么办?你可以“切换”下拉框的状态,你可以使用"setSelected"方法去做一些事情,比如选择下拉列表,处理’SELECT’元素其实很简单。

element = driver.find_element_by_xpath("//select[@name='name']")
all_options = element.find_elements_by_tag_name("option")
for option in all_options:
	print("Value is: %s" % option.get_attribute("value"))
	option.click()

上面这段代码将会寻找页面第一个“SELECT”元素,并且循环它的每一个option元素,打印出它们的值,然后按顺序都选中一遍。

As you can see, this isn’t the most efficient way of dealing with SELECT elements. WebDriver’s support classes include one called a “Select”, which provides useful methods for interacting with these:

from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_name('name'))
select.select_by_index(index)
select.select_by_visible_text("text")
select.select_by_value(value)

WebDriver also provides features for deselecting all the selected options:

select = Select(driver.find_element_by_id('id'))
select.deselect_all()

This will deselect all OPTIONs from that particular SELECT on the page.
Suppose in a test, we need the list of all default selected options, Select class provides a property method that returns a list:

select = Select(driver.find_element_by_xpath("//select[@name='name']")
all_selected_options = select.all_selected_options

To get all available options:

options = select.options

Once you’ve finished filling out the form, you probably want to submit it. One way to do this would be to find the ‘submit’ button an click it:

# Assume the button has the ID "submit"
driver.find_element_by_id("submit").click()

Alternatively, WebDriver has the convenience method “submit” on every element. If you call this on an element within a form, WebDriver will walk up the DOM until it finds the enclosing form and the calls submit on that. If the element isn’t in a form, then the NoSuchElementException will be raised:

element.submit()

Drag and drop(拖放)

You can use drag and drop, either moving an element by a certain amount, or on to another element:

element = driver.find_element_by_name("source")
target = driver.find_element_by_name("target")

from selenium.webdriver import ActionChains
action_chains = ActionChains(driver)
action_chains.drag_and_drop(element, target).perform()

Moving between windows and frames(在不同的窗口和框架之间移动)

It’s rare for a modern web application not to have any frames or to be constrained to a single window. WebDriver supports moving between named windows using the “switch_to_window” method:

driver.switch_to_window("windowName")

All calls to driver will now be interpreted as being directed to the particular window. But how do you know the window’s name? Take a look at the javascript or link that opened it:

<a href="somewhere.html" target="windowName'>Click here to open a new window</a>

猜你喜欢

转载自blog.csdn.net/htsait4113/article/details/84324594