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)})

猜你喜欢

转载自blog.csdn.net/rubikchen/article/details/84308869