import time
from selenium import webdriver
from selenium.webdriver.chrome import options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import os
current_path=os.path.dirname(__file__)
driver_path=os.path.join(current_path,'./webdriver/chromedriver')
#无头浏览器:不显示浏览器UI的情况下运行基于UI的浏览器测试,即不需要用户界面的浏览器
#优点:
#1.无头浏览器比真正的浏览器更快。原因在于您没有启动浏览器GUI,所以您可以绕过真正的浏览器加载CSS、JavaScript、打开和呈现HTML所花费的所有时间。
#2.利用无头浏览器爬网站数据,因为您只是寻找你想要的数据,所以没有必要启动一个完整的浏览器实例,开销越少,返回结果的速度就越快。
#3.无头浏览器脚本监视网络应用程序的性能。
#应用场景:
#1.在没有界面的机器上运行测试。
#2.在一台机器上模拟多个浏览器。
#3.可以在无界面的服务器或CI上运行测试,减少了外界的干扰,使自动化测试更稳定。
#无头浏览器设置
# options=options.Options()
# options.add_argument('--headless')
# driver=webdriver.Chrome(executable_path=driver_path,options=options)
#取消chrome受自动控制提示
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
driver=webdriver.Chrome(executable_path=driver_path,options=chrome_options)
driver.get('http://www.baidu.com')
print(driver.title)
WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID,"kw")))
driver.find_element(By.ID,"kw").send_keys('test')
driver.find_element(By.ID,"su").click()
time.sleep(2)
print(driver.title)