1. 程式人生 > >pyhon學習之selenium登陸微博

pyhon學習之selenium登陸微博

登陸微博

#-*- encoding=utf-8 -*-
from selenium import webdriver
import time
browser = webdriver.Chrome()
browser.get("https://weibo.com")
browser.maximize_window()
time.sleep(6)
browser.find_element_by_id("loginname").send_keys("使用者名稱")
browser.find_elements_by_name("password")[0].send_keys("密碼"+"\n")
#browser.quit()

send_keys是使用在form表單裡面,我們可以看一下原始碼。

def send_keys(self, *value):
	"""Simulates typing into the element.

	:Args:
		- value - A string for typing, or setting form fields.  For setting
		  file inputs, this could be a local file path.

	Use this to send simple key events or to fill out form fields::

		form_textfield = driver.find_element_by_name('username')
		form_textfield.send_keys("admin")

	This can also be used to set file inputs.

	::

		file_input = driver.find_element_by_name('profilePic')
		file_input.send_keys("path/to/profilepic.gif")
		# Generally it's better to wrap the file path in one of the methods
		# in os.path to return the actual path to support cross OS testing.
		# file_input.send_keys(os.path.abspath("path/to/profilepic.gif"))

	"""
	# transfer file to another machine only if remote driver is used
	# the same behaviour as for java binding
	if self.parent._is_remote:
		local_file = self.parent.file_detector.is_local_file(*value)
		if local_file is not None:
			value = self._upload(local_file)

	self._execute(Command.SEND_KEYS_TO_ELEMENT,
				  {'text': "".join(keys_to_typing(value)),
				   'value': keys_to_typing(value)})