Python爬虫之selenium库(六):获取一组元素_自动填写用户名和密码

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

先前在定位元素时,我们使用的是find_element_的方法,如果要获取一组元素可以使用find_elements_方法

这里以百度文库为例,获取所有的input标签,并且从中筛选出单选框并点击value为2的框

chrome = webdriver.Chrome()
chrome.get("https://wenku.baidu.com/")
inputs = chrome.find_elements_by_tag_name("input")
for input_ in inputs:
    if input_.get_attribute("type") == "radio" and input_.get_attribute("value")=="2":
        input_.click()
time.sleep(5)
chrome.close()

自定填写用户名和密码

chrome = webdriver.Chrome()
chrome.get("https://mail.pku.edu.cn/")
inputs = chrome.find_elements_by_tag_name("input")
for input_ in inputs:
    if input_.get_attribute("type") == "text":
        input_.send_keys("username")
    if input_.get_attribute("type") == "password":
        input_.send_keys("password")
time.sleep(5)
chrome.close()

猜你喜欢

转载自blog.csdn.net/bqw18744018044/article/details/81388781