最近朋友在學(xué)Selenium的時(shí)候遇到一個(gè)問題,當(dāng)執(zhí)行完selenium程序后,瀏覽器會(huì)閃退也就是自動(dòng)關(guān)閉,程序中也沒有寫driver.quit()或driver.close()方法,解決后發(fā)布上來以供學(xué)習(xí)交流。
內(nèi)容比較簡(jiǎn)單,這里直接附上代碼:
(1)修改前文章來源:http://www.zghlxwxcb.cn/news/detail-501862.html
from selenium import webdriver
from selenium.webdriver.common.by import By
bro = webdriver.Chrome(executable_path='D:/項(xiàng)目/chromedriver.exe')
def get_url():
bro.get('https://www.jd.com/')
search_input = bro.find_element(By.ID, 'key') # 定位搜索框
search_input.send_keys('創(chuàng)維電視')
bro.find_element(By.XPATH, '//*[@id="search"]/div/div[2]/button').click() # 點(diǎn)擊搜索
if __name__ == '__main__':
get_url()
(2)修改后:文章來源地址http://www.zghlxwxcb.cn/news/detail-501862.html
from selenium import webdriver
from selenium.webdriver.common.by import By
# ***********關(guān)鍵行***********
option = webdriver.ChromeOptions()
option.add_experimental_option("detach", True)
bro = webdriver.Chrome(executable_path='D:/項(xiàng)目/chromedriver.exe', options=option)
# ***********關(guān)鍵行結(jié)束***********
def get_url():
bro.get('https://www.jd.com/')
search_input = bro.find_element(By.ID, 'key') # 定位搜索框
search_input.send_keys('創(chuàng)維電視')
bro.find_element(By.XPATH, '//*[@id="search"]/div/div[2]/button').click() # 點(diǎn)擊搜索
if __name__ == '__main__':
get_url()
到了這里,關(guān)于Python運(yùn)行selenium程序執(zhí)行結(jié)束后自動(dòng)關(guān)閉瀏覽器 解決方法 親測(cè)有效的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!