9
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Tôi đang cố gắng sử dụng ví dụ Firefox/Selenium làm trình chiếu thô sơ cho hình ảnh. Ý tưởng là tôi sẽ mở các tệp webdriver
và driver.get[]
từ một thư mục cục bộ.
Khi tôi chạy như sau, tôi nhận được một lỗi: selenium.common.exceptions.WebDriverException: Message: Tried to run command without establishing a connection
Giả định của tôi là Selenium đang cố gắng kiểm tra yêu cầu driver.get[]
tiếp theo và không cho phép kết nối cục bộ, không kết nối web, có cách nào để bỏ qua hành vi này không? Ví dụ mã của tôi xuất hiện dưới đây:
from selenium import webdriver
import time
from os import listdir
from selenium.common.exceptions import WebDriverException
driver = webdriver.Firefox[]
image_source = '/home/pi/Desktop/slideshow/photo_frames/daniel/images/'
for file in listdir[image_source]:
if file.endswith['jpg']:
file_name = image_source + file
driver.get[file_name]
time.sleep[5]
Cập nhật: Tôi nên thêm rằng cấu trúc tập lệnh cơ bản tương tự hoạt động cho các trang web - tôi có thể lặp qua một số trang web mà không có bất kỳ lỗi nào.
sa mạc
54,7K20 Huy hiệu vàng129 Huy hiệu bạc160 Huy hiệu đồng20 gold badges129 silver badges160 bronze badges
Đã hỏi ngày 22 tháng 3 năm 2017 lúc 11:49Mar 22, 2017 at 11:49
Tôi nghĩ rằng bạn chỉ cần thêm file://
vào tên tệp. Điều này làm việc cho tôi:
from selenium import webdriver
import time
from os import listdir
from selenium.common.exceptions import WebDriverException
def main[]:
image_source = '/home/pi/Desktop/slideshow/photo_frames/daniel/images/'
driver = webdriver.Firefox[]
try:
for file in listdir[image_source]:
if file.endswith['jpg']:
file_name = 'file://' + image_source + file
driver.get[file_name]
time.sleep[5]
finally:
driver.quit[]
if __name__ == "__main__":
main[]
Đã trả lời ngày 24 tháng 3 năm 2017 lúc 15:14Mar 24, 2017 at 15:14
Levi Noeckerlevi NoeckerLevi Noecker
2.9121 Huy hiệu vàng15 Huy hiệu bạc28 Huy hiệu đồng1 gold badge15 silver badges28 bronze badges
2
Nếu bạn đến đây muốn Selenium phục vụ tệp HTML địa phương của bạn, như tôi đã làm, câu trả lời được chấp nhận ở trên cần một sửa đổi nhỏ để nó hoạt động, theo chú ý của Niklas Rosencrantz.
Để Selenium phục vụ HTML cục bộ trong trình duyệt của bạn và giả sử tệp nằm trong thư mục làm việc hiện tại của bạn, hãy thử điều này [tôi đang ở trên Windows, sử dụng Selenium 3.141.0 và Python 3.7 - Nếu nó quan trọng với bạn]:
from selenium import webdriver
import os
browser = webdriver.Firefox[]
html_file = os.getcwd[] + "//" + "relative//path//to//file.html"
browser.get["file:///" + html_file]
Đã trả lời ngày 17 tháng 11 năm 2018 lúc 1:38Nov 17, 2018 at 1:38
Adriaanbdadriaanbdadriaanbd
2873 Huy hiệu bạc10 Huy hiệu đồng3 silver badges10 bronze badges
Điều này cũng có thể được thực hiện với Pathlib
from selenium import webdriver
from pathlib import Path
browser = webdriver.Firefox[]
html_file = Path.cwd[] / "relative//path//to//file.html"
browser.get[html_file.as_uri[]]
Nếu bạn chưa quen với Pathlib thì/cú pháp có thể trông hơi kỳ quặc, nhưng nó rất dễ sử dụng, đây là một hướng dẫn tốt //realpython.com/python-pathlib/
Đã trả lời ngày 8 tháng 1 năm 2019 lúc 8:58Jan 8, 2019 at 8:58
Dan Heauforddan HeartfordDan Heaford
4213 Huy hiệu bạc10 Huy hiệu đồng3 silver badges10 bronze badges
Chỉnh sửa từ Anwser của Dan Healford để mở tệp HTML cục bộ:
HTML:
from selenium import webdriver
import time
from os import listdir
from selenium.common.exceptions import WebDriverException
def main[]:
image_source = '/home/pi/Desktop/slideshow/photo_frames/daniel/images/'
driver = webdriver.Firefox[]
try:
for file in listdir[image_source]:
if file.endswith['jpg']:
file_name = 'file://' + image_source + file
driver.get[file_name]
time.sleep[5]
finally:
driver.quit[]
if __name__ == "__main__":
main[]
0from selenium.webdriver.common.by import By
from selenium import webdriver
from pathlib import Path
chrome_driver_path = "#'** Dir where you saved your
chromedriver.exe**'/Users/ASUS/chromedriver"
browser = webdriver.Chrome[executable_path=chrome_driver_path]
html_file = Path.cwd[] / "#html file Dir.html"
browser.get[html_file.as_uri[]]
heading1 = browser.find_element[By.TAG_NAME, 'h2']
print[heading1.text]
result in console - "I'm an h2"
sa mạc
54,7K20 Huy hiệu vàng129 Huy hiệu bạc160 Huy hiệu đồng20 gold badges129 silver badges160 bronze badges
Đã hỏi ngày 22 tháng 3 năm 2017 lúc 11:49Sep 27 at 9:08