import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By #自动下载驱动 from webdriver_helper import get_webdriver # 创建一个参数对象,用来控制chrome以无界面模式打开(可以视为固定写法) opt = Options() #新建参数对象 opt.add_argument("--headless") #无头参数 opt.add_argument("--disbale-gpu") #无gpu图形化界面 print('open chrome') #无界面运行 # driver = webdriver.Chrome(options=opt) driver = get_webdriver(options=opt) #界面运行 # driver = get_webdriver() print('open bilibili') driver.get("https://www.bilibili.com") driver.maximize_window() print('select element') # 找到输入框位置,发送字符进行点击搜索按钮 driver.find_element(By.CLASS_NAME, 'nav-search-input').send_keys("Uipath") driver.find_element(By.CLASS_NAME, 'nav-search-btn').click() # 获取所有窗口的句柄 arrs = driver.window_handles # 切换到最后一个窗口 driver.switch_to.window(arrs[len(arrs) - 1]) title = driver.find_element(By.XPATH, '//*[@id="i_cecream"]/div/div[2]/div[2]/div/div/div/div[2]/div/div[1]/div/div[2]/div/div/a/h3').get_attribute('title') print(title) time.sleep(3) driver.refresh() print('sleep') time.sleep(3)
标签:opt,webdriver,get,python,Demo,driver,div,selenium From: https://www.cnblogs.com/aaronL/p/18075964