function - driver.get(url) 有时不能循环工作
问题描述
有时 driver.get(url) 函数在没有任何错误和日志的情况下无法循环工作。
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument("--disable-webgl")
options.add_experimental_option("excludeSwitches", ["enable-logging"])
path = r'chromedriver.exe'
driver = webdriver.Chrome(path,options=options)
for x in range(1000):
url = "https://www.palabrasaleatorias.com/random-words.php"
driver.get(url)
url = "https://www.google.ru/imghp?hl=ru"
print(x,"Test[1]")
driver.get(url)
print(x,"Test[2]")
driver.close()
是功能 driver.get 的硒错误还是什么?
解决方案
请尝试添加一些睡眠语句(以防 url 需要一些时间来加载):
import time
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument("--disable-webgl")
options.add_experimental_option("excludeSwitches", ["enable-logging"])
path = r'chromedriver.exe'
driver = webdriver.Chrome(path,options=options)
for x in range(1000):
url = "https://www.palabrasaleatorias.com/random-words.php"
driver.get(url)
# waiting for 3 seconds for the url to load
time.sleep(5)
url = "https://www.google.ru/imghp?hl=ru"
# waiting for 3 seconds for the next url to load
time.sleep(3)
print(x, "Test[1]")
driver.get(url)
print(x, "Test[2]")
driver.close()