from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=options)
driver.get('http://www.baidu.com')
element = driver.find_element_by_css_selector('#su')
print(element.get_attribute('value'))
driver.quit()
以上是
上面我们知道了selenium支持很多的浏览器,但是如果想要声明并调用浏览器,
这里列举一下常用的查找元素方法:
find_element_by_name
find_element_by_id
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector
import time
driver = webdriver.Chrome()
driver.maximize_window()
driver.get('http://www.baidu.com')
time.sleep(1)
search_input = driver.find_element_by_css_selector('input#kw')
search_input.send_keys('python')
time.sleep(1)
submit = driver.find_element_by_css_selector('input#su')
submit.click()
time.sleep(1)
print(driver.current_url)
results = driver.find_elements_by_css_selector('h3 > a')
for result in results:
print(result.text)
结果如下:
硅谷热门Python零基础入门课程_转战名企薪资翻倍 python的学习_从入门到精通-网易云课堂 python课程一站式-包含自动化测试和接口测试 Welcome to Python.org 官网 Python_百度百科 Python 基础教程 | 菜鸟教程 Python教程 - 廖雪峰的官方网站 Download Python | Python.org Python 简介 | 菜鸟教程 Python 的练手项目有哪些值得推荐? - 知乎 Our Documentation | Python.org PythonTab:Python中文开发者社区门户 Python3 教程 | 菜鸟教程 python是什么?如何快速学习python?前景怎么样? 【千锋教育】python_人工智能+Python全栈+数据分析+项目实战..
from selenium import webdriver
import time
def douban():
while True:
driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://accounts.douban.com/login')
time.sleep(1)
search_input1 = driver.find_element_by_css_selector('input#email')
search_input1.send_keys('[email protected]')
time.sleep(1)
search_input2 = driver.find_element_by_css_selector('input#password')
search_input2.send_keys('aaa')
captcha = driver.find_element_by_css_selector('img#captcha_image')
submit = driver.find_element_by_css_selector('.btn-submit')
if captcha:
captcha_field = driver.find_element_by_css_selector('input#captcha_field')
text = input("please input captcha")
captcha_field.send_keys(text)
submit.click()
else:
submit.click()
douban()