1.正常使用chrome驅動打開一個網頁,正常訪問
from selenium.webdriver import Chrome
web = Chrome()
web.get("http://www.chaojiying.com/user/login/")
2.在使用selenium測試一個自動登錄的程序,測試了很長時間,一直是閃退
chrome版本:版本 99.0.4844.51(正式版本) (64 位)
chromedriver版本:99.0.4844.51
seleniium版本:4.0+
from selenium.webdriver import Chrome
from chaojiying import Chaojiying_Client
from selenium.webdriver.common.by import By
import time
web = Chrome()
web.get("http://www.chaojiying.com/user/login/")
time.sleep(5) # Let the user actually see something!
# 處理驗證碼
img = web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/div/img').screenshot_as_png
#登錄超級鷹
chaojiying = Chaojiying_Client('18312341234', '123456', '912345')
dic = chaojiying.PostPic(img,1902)
verify_code = dic['pic_str']
# 想頁面中填入用戶名,密碼驗證碼
web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[1]/input').send_keys("18312341234")
web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[2]/input').send_keys("123456")
web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[3]/input').send_keys(verify_code)
#點擊登錄
time.sleep(2)
web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[4]/input').click()
# driver.quit()
3.測試指定瀏覽器驅動位置
看網上的教程是,沒有指定chromedrive.exe的環境變量,或者chrome的內核版本跟chromedrive版本不一致,兩種方式都進行了重試,然后在重裝,仍然沒用,這里是指定chromedrive.exe的代碼部分
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
service = Service(r"D:\WebSpider\venv\Scripts\geckodriver.exe")
service.start()
driver = webdriver.Remote(service.service_url)
driver.get('http://www.chaojiying.com/user/login/')
官方文檔中給到的介紹,指定chromedrive的路徑,但是實測通過這種方式打開網頁還是閃退,不清楚是不是網站針對chrome做的反爬手段。
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service('/path/to/chromedriver')
service.start()
driver = webdriver.Remote(service.service_url)
driver.get('http://www.google.com/');
time.sleep(5) # Let the user actually see something!
driver.quit()
4.嘗試更換瀏覽器為火狐的,使用最新版的火狐瀏覽器,直接對應也是最新的火狐驅動
firefox驅動下載鏈接:https://gitHub.com/mozilla/geckodriver/releases
火狐瀏覽器版本:101.0.1 (64 位)
火狐驅動版本:0.31.0 (2022-04-11, b617178ef491)
嘗試使用指定火狐驅動打開之前寫的程序,測試成功,問題還是出在了chrome瀏覽器中
from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
from chaojiying import Chaojiying_Client
import time
web = Firefox()
web.get("http://www.chaojiying.com/user/login/")
# 處理驗證碼
img = web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/div/img').screenshot_as_png
chaojiying = Chaojiying_Client('18312341234', '123456', '912345')
dic = chaojiying.PostPic(img,1902)
verify_code = dic['pic_str']
# 想頁面中填入用戶名,密碼驗證碼
web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[1]/input').send_keys("183312341234")
web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[2]/input').send_keys("123456")
web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[3]/input').send_keys(verify_code)
#點擊登錄
time.sleep(5)
web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[4]/input').click()
# driver.quit
也可以使用指定火狐驅動器來執行。
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.by import By
from chaojiying import Chaojiying_Client
import time
service = Service(r"D:\WebSpider\venv\Scripts\geckodriver.exe")
service.start()
driver = webdriver.Remote(service.service_url)
driver.get('http://www.chaojiying.com/user/login/');
time.sleep(5) # Let the user actually see something!
# 處理驗證碼
img = driver.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/div/img').screenshot_as_png
chaojiying = Chaojiying_Client('18312341234', '123456', '912345')
dic = chaojiying.PostPic(img,1902)
verify_code = dic['pic_str']
# 想頁面中填入用戶名,密碼驗證碼
driver.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[1]/input').send_keys("18312341234")
driver.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[2]/input').send_keys("123456")
driver.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[3]/input').send_keys(verify_code)
#點擊登錄
time.sleep(5)
driver.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[4]/input').click()
# driver.quit()
5.針對chrome閃退,是driver的全局變量問題,以下方法經測試確實可以解決selenium打開chrome閃退的問題。
1、不設置driver為全局,放在函數內(會閃退)
from selenium import webdriver
# 登陸百度
def main():
chromedriver_path = r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
driver = webdriver.Chrome(executable_path=chromedriver_path)
# 打開頁面
page = driver.get('https://www.baidu.com/')
if __name__ == "__main__":
main()
2、把driver放在函數外,為全局(不會閃退)
from selenium import webdriver
chromedriver_path = r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
driver = webdriver.Chrome(executable_path=chromedriver_path)
# 登陸百度
def main():
# 打開頁面
page = driver.get('https://www.baidu.com/')
if __name__ == "__main__":
main()
3、也可以把driver放在函數內,只要設置為全局變量就可以
from selenium import webdriver
# 登陸百度
def main():
global driver
chromedriver_path = r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
driver = webdriver.Chrome(executable_path=chromedriver_path)
# 打開頁面
page = driver.get('https://www.baidu.com/')
if __name__ == "__main__":
main()
本文內容不用于商業目的,如涉及知識產權問題,請權利人聯系51Testing小編(021-64471599-8017),我們將立即處理