版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xcd1997/article/details/82500463
from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver import ActionChains import time #创建浏览器对象 d = webdriver.Chrome()#可以看到浏览器界面 d.implicitly_wait(10)#隐性等待10秒如果10秒内该窗口完成渲染渲染完毕就不再等待,10秒还未渲染成功就不再等待 # from selenium.webdriver.chrome.options import Options # chrome_options = Options() # chrome_options.add_argument('--headless') # chrome_options.add_argument('--disable-gpu') # d = webdriver.Chrome(chrome_options=chrome_options)#创建浏览器界面但是不显示界面 url = 'https://www.baidu.com' d.get(url)#发送请求获取界面 node = d.find_element_by_xpath('//*[@id="kw"]')#根据xpath进行获取对象(百度搜索框) butten = d.find_element_by_xpath('//*[@id="su"]')#获取到按钮对象 node.send_keys('吴秀波')#向搜索框添加搜索内容 su = butten.click()#模拟点击搜索按钮 print(d.window_handles)#输出打开的窗口 d.switch_to.window(d.window_handles[0])#跳转窗口 #find_elements_by_xpath获取的是列表,不能直接使用必须取出 node1 = d.find_elements_by_xpath('//*[@id="1"]/h3/a') print(type(node1[0])) #输出node1的href属性 print(node1[0].get_attribute('href')) #模拟鼠标点击该控件 time.sleep(3) ActionChains(d).move_to_element(node1[0]).double_click().perform() time.sleep(1) d.close() d.quit()