from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
driver.get("http:www.baidu.com")
driver.find_element(By.NAME,'wd').send_keys("hello") #通过Name定位百度搜索框
sleep(3)
driver.find_element(By.ID,'su').click() #通过ID定位“百度一下”按钮
sleep(3)
driver.back()
sleep(2)
driver.find_element(By.CLASS_NAME,'s_ipt').send_keys("Python+selenium自动化测试") #通过Name定位百度搜索框
sleep(3)
driver.find_element(By.ID,'su').click() #通过ID定位“百度一下”按钮
sleep(3)
driver.back()
sleep(2)
driver.find_element(By.LINK_TEXT,'新闻').click() #通过Link_text定位到百度首页的“新闻”超链接
sleep(2)
driver.close()
print("关闭当前页")
sleep(2)
driver.back()
sleep(2)
driver.find_element(By.CLASS_NAME,'title-content-title').click() #通过Class定位到百度首页的热门新闻链接
print("成功打开热门新闻")
sleep(2)
driver.find_element(By.XPATH,"//input[@id='kw']").send_keys("Python") #通过XPath定位到百度的搜索框
driver.find_element(By.ID,'su').click()
sleep(2)
driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("test") #通过CSS_selector来定位百度的搜索框
driver.find_element(By.ID,'su').click()
sleep(2)