Hướng dẫn how do you check if web page is loaded completely in selenium python? - làm cách nào để kiểm tra xem trang web có được tải hoàn toàn bằng trăn selen không?

Tôi muốn cạo tất cả dữ liệu của một trang được thực hiện bởi một cuộn vô hạn. Mã Python sau đây hoạt động.

for i in range(100):
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(5)

Điều này có nghĩa là mỗi khi tôi cuộn xuống phía dưới, tôi cần phải đợi 5 giây, thường là đủ để trang hoàn thành tải các nội dung mới được tạo. Nhưng, điều này có thể không hiệu quả về thời gian. Trang có thể hoàn thành tải nội dung mới trong vòng 5 giây. Làm thế nào tôi có thể phát hiện xem trang hoàn thành tải nội dung mới mỗi khi tôi cuộn xuống? Nếu tôi có thể phát hiện điều này, tôi có thể cuộn xuống một lần nữa để xem thêm nội dung sau khi tôi biết trang hoàn thành tải. Đây là thời gian hiệu quả hơn.

Hướng dẫn how do you check if web page is loaded completely in selenium python? - làm cách nào để kiểm tra xem trang web có được tải hoàn toàn bằng trăn selen không?

Moshe Slavin

5,0075 Huy hiệu vàng23 Huy hiệu bạc36 Huy hiệu Đồng5 gold badges23 silver badges36 bronze badges

Hỏi ngày 25 tháng 10 năm 2014 lúc 20:14Oct 25, 2014 at 20:14

4

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()
driver.get('url')
timeout = 5
try:
    element_present = EC.presence_of_element_located((By.ID, 'element_id'))
    WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
    print "Timed out waiting for page to load"
2 sẽ đợi một trang tải theo mặc định thông qua phương thức
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()
driver.get('url')
timeout = 5
try:
    element_present = EC.presence_of_element_located((By.ID, 'element_id'))
    WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
    print "Timed out waiting for page to load"
3.

Như bạn có thể đang tìm kiếm một số yếu tố cụ thể như @user227215 đã nói, bạn nên sử dụng

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()
driver.get('url')
timeout = 5
try:
    element_present = EC.presence_of_element_located((By.ID, 'element_id'))
    WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
    print "Timed out waiting for page to load"
4 để chờ một phần tử nằm trong trang của bạn:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException

browser = webdriver.Firefox()
browser.get("url")
delay = 3 # seconds
try:
    myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))
    print "Page is ready!"
except TimeoutException:
    print "Loading took too much time!"

Tôi đã sử dụng nó để kiểm tra cảnh báo. Bạn có thể sử dụng bất kỳ phương pháp loại nào khác để tìm Trình định vị.

Chỉnh sửa 1:

Tôi nên đề cập rằng

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()
driver.get('url')
timeout = 5
try:
    element_present = EC.presence_of_element_located((By.ID, 'element_id'))
    WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
    print "Timed out waiting for page to load"
2 sẽ đợi một trang tải theo mặc định. Nó không chờ đợi để tải bên trong các khung hoặc cho các yêu cầu AJAX. Điều đó có nghĩa là khi bạn sử dụng
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()
driver.get('url')
timeout = 5
try:
    element_present = EC.presence_of_element_located((By.ID, 'element_id'))
    WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
    print "Timed out waiting for page to load"
6, trình duyệt của bạn sẽ đợi cho đến khi trang hoàn toàn được tải và sau đó chuyển đến lệnh tiếp theo trong mã. Nhưng khi bạn đang đăng một yêu cầu AJAX,
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()
driver.get('url')
timeout = 5
try:
    element_present = EC.presence_of_element_located((By.ID, 'element_id'))
    WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
    print "Timed out waiting for page to load"
2 không chờ đợi và bạn có trách nhiệm chờ đợi một lượng thời gian thích hợp cho trang hoặc một phần của trang để tải; Vì vậy, có một mô -đun có tên
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()
driver.get('url')
timeout = 5
try:
    element_present = EC.presence_of_element_located((By.ID, 'element_id'))
    WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
    print "Timed out waiting for page to load"
8.

Hướng dẫn how do you check if web page is loaded completely in selenium python? - làm cách nào để kiểm tra xem trang web có được tải hoàn toàn bằng trăn selen không?

Đã trả lời ngày 25 tháng 10 năm 2014 lúc 21:44Oct 25, 2014 at 21:44

Hướng dẫn how do you check if web page is loaded completely in selenium python? - làm cách nào để kiểm tra xem trang web có được tải hoàn toàn bằng trăn selen không?

13

Cố gắng chuyển

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()
driver.get('url')
timeout = 5
try:
    element_present = EC.presence_of_element_located((By.ID, 'element_id'))
    WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
    print "Timed out waiting for page to load"
9 cho hàm tạo cho
def page_has_loaded(self):
    self.log.info("Checking if {} page is loaded.".format(self.driver.current_url))
    page_state = self.driver.execute_script('return document.readyState;')
    return page_state == 'complete'
0 (như thể hiện trong câu trả lời được chấp nhận) đã khiến
def page_has_loaded(self):
    self.log.info("Checking if {} page is loaded.".format(self.driver.current_url))
    page_state = self.driver.execute_script('return document.readyState;')
    return page_state == 'complete'
1 được nâng lên. Tôi đã phải sử dụng cú pháp trong nhận xét của Fragles:

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()
driver.get('url')
timeout = 5
try:
    element_present = EC.presence_of_element_located((By.ID, 'element_id'))
    WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
    print "Timed out waiting for page to load"

Điều này phù hợp với ví dụ trong tài liệu. Dưới đây là một liên kết đến tài liệu cho.

Đã trả lời ngày 18 tháng 5 năm 2016 lúc 14:49May 18, 2016 at 14:49

4

Tìm bên dưới 3 phương pháp:

ReadyState

Kiểm tra trang ReadyState (không đáng tin cậy):

def page_has_loaded(self):
    self.log.info("Checking if {} page is loaded.".format(self.driver.current_url))
    page_state = self.driver.execute_script('return document.readyState;')
    return page_state == 'complete'

Hàm trợ giúp

def page_has_loaded(self):
    self.log.info("Checking if {} page is loaded.".format(self.driver.current_url))
    page_state = self.driver.execute_script('return document.readyState;')
    return page_state == 'complete'
2 là tốt, nhưng thật không may,
def page_has_loaded(self):
    self.log.info("Checking if {} page is loaded.".format(self.driver.current_url))
    page_state = self.driver.execute_script('return document.readyState;')
    return page_state == 'complete'
3 mở ra điều kiện cuộc đua nơi chúng tôi quản lý để thực hiện tập lệnh trong trang cũ, trước khi trình duyệt bắt đầu xử lý nhấp chuột và
def page_has_loaded(self):
    self.log.info("Checking if {} page is loaded.".format(self.driver.current_url))
    page_state = self.driver.execute_script('return document.readyState;')
    return page_state == 'complete'
4 chỉ trả về đúng.

def page_has_loaded(self): self.log.info("Checking if {} page is loaded.".format(self.driver.current_url)) page_state = self.driver.execute_script('return document.readyState;') return page_state == 'complete' 5

So sánh ID trang mới với trang cũ:

def page_has_loaded_id(self):
    self.log.info("Checking if {} page is loaded.".format(self.driver.current_url))
    try:
        new_page = browser.find_element_by_tag_name('html')
        return new_page.id != old_page.id
    except NoSuchElementException:
        return False

Có thể việc so sánh ID không hiệu quả bằng việc chờ đợi các ngoại lệ tham khảo cũ.

def page_has_loaded(self): self.log.info("Checking if {} page is loaded.".format(self.driver.current_url)) page_state = self.driver.execute_script('return document.readyState;') return page_state == 'complete' 6

Sử dụng phương thức

def page_has_loaded(self):
    self.log.info("Checking if {} page is loaded.".format(self.driver.current_url))
    page_state = self.driver.execute_script('return document.readyState;')
    return page_state == 'complete'
6:

@contextlib.contextmanager
def wait_for_page_load(self, timeout=10):
    self.log.debug("Waiting for page to load at {}.".format(self.driver.current_url))
    old_page = self.find_element_by_tag_name('html')
    yield
    WebDriverWait(self, timeout).until(staleness_of(old_page))

Để biết thêm chi tiết, hãy kiểm tra blog của Harry.

Đã trả lời ngày 21 tháng 5 năm 2015 lúc 23:09May 21, 2015 at 23:09

Kenorbkenorbkenorb

Phù bằng vàng 145K7676 gold badges656 silver badges714 bronze badges

2

Như đã đề cập trong câu trả lời từ David Cullen, tôi luôn thấy các đề xuất sử dụng một dòng như sau:

element_present = EC.presence_of_element_located((By.ID, 'element_id'))
WebDriverWait(driver, timeout).until(element_present)

Thật khó để tôi tìm thấy ở đâu đó tất cả các trình định vị có thể có thể được sử dụng với

def page_has_loaded(self):
    self.log.info("Checking if {} page is loaded.".format(self.driver.current_url))
    page_state = self.driver.execute_script('return document.readyState;')
    return page_state == 'complete'
8, vì vậy tôi nghĩ rằng sẽ rất hữu ích khi cung cấp danh sách ở đây. Theo Web Scraping với Python của Ryan Mitchell:Web Scraping with Python by Ryan Mitchell:

def page_has_loaded(self):
    self.log.info("Checking if {} page is loaded.".format(self.driver.current_url))
    page_state = self.driver.execute_script('return document.readyState;')
    return page_state == 'complete'
9

Được sử dụng trong ví dụ; Tìm các phần tử theo thuộc tính ID HTML của họ

def page_has_loaded_id(self):
    self.log.info("Checking if {} page is loaded.".format(self.driver.current_url))
    try:
        new_page = browser.find_element_by_tag_name('html')
        return new_page.id != old_page.id
    except NoSuchElementException:
        return False
0

Được sử dụng để tìm các phần tử theo thuộc tính lớp HTML của họ. Tại sao chức năng này

def page_has_loaded_id(self):
    self.log.info("Checking if {} page is loaded.".format(self.driver.current_url))
    try:
        new_page = browser.find_element_by_tag_name('html')
        return new_page.id != old_page.id
    except NoSuchElementException:
        return False
0 không chỉ đơn giản là
def page_has_loaded_id(self):
    self.log.info("Checking if {} page is loaded.".format(self.driver.current_url))
    try:
        new_page = browser.find_element_by_tag_name('html')
        return new_page.id != old_page.id
    except NoSuchElementException:
        return False
2? Sử dụng biểu mẫu
def page_has_loaded_id(self):
    self.log.info("Checking if {} page is loaded.".format(self.driver.current_url))
    try:
        new_page = browser.find_element_by_tag_name('html')
        return new_page.id != old_page.id
    except NoSuchElementException:
        return False
3 sẽ tạo ra các vấn đề cho thư viện Java của Selenium, trong đó
def page_has_loaded_id(self):
    self.log.info("Checking if {} page is loaded.".format(self.driver.current_url))
    try:
        new_page = browser.find_element_by_tag_name('html')
        return new_page.id != old_page.id
    except NoSuchElementException:
        return False
4 là phương pháp dành riêng. Để giữ cho cú pháp selen phù hợp giữa các ngôn ngữ khác nhau,
def page_has_loaded_id(self):
    self.log.info("Checking if {} page is loaded.".format(self.driver.current_url))
    try:
        new_page = browser.find_element_by_tag_name('html')
        return new_page.id != old_page.id
    except NoSuchElementException:
        return False
0 đã được sử dụng thay thế.

def page_has_loaded_id(self):
    self.log.info("Checking if {} page is loaded.".format(self.driver.current_url))
    try:
        new_page = browser.find_element_by_tag_name('html')
        return new_page.id != old_page.id
    except NoSuchElementException:
        return False
6

Tìm các phần tử theo lớp, ID hoặc tên thẻ của họ, sử dụng quy ước

def page_has_loaded_id(self):
    self.log.info("Checking if {} page is loaded.".format(self.driver.current_url))
    try:
        new_page = browser.find_element_by_tag_name('html')
        return new_page.id != old_page.id
    except NoSuchElementException:
        return False
7,
def page_has_loaded_id(self):
    self.log.info("Checking if {} page is loaded.".format(self.driver.current_url))
    try:
        new_page = browser.find_element_by_tag_name('html')
        return new_page.id != old_page.id
    except NoSuchElementException:
        return False
8,
def page_has_loaded_id(self):
    self.log.info("Checking if {} page is loaded.".format(self.driver.current_url))
    try:
        new_page = browser.find_element_by_tag_name('html')
        return new_page.id != old_page.id
    except NoSuchElementException:
        return False
9.

@contextlib.contextmanager
def wait_for_page_load(self, timeout=10):
    self.log.debug("Waiting for page to load at {}.".format(self.driver.current_url))
    old_page = self.find_element_by_tag_name('html')
    yield
    WebDriverWait(self, timeout).until(staleness_of(old_page))
0

Tìm thẻ HTML bằng văn bản chúng chứa. Ví dụ: một liên kết có thể chọn "tiếp theo" có thể được chọn bằng

@contextlib.contextmanager
def wait_for_page_load(self, timeout=10):
    self.log.debug("Waiting for page to load at {}.".format(self.driver.current_url))
    old_page = self.find_element_by_tag_name('html')
    yield
    WebDriverWait(self, timeout).until(staleness_of(old_page))
1.

@contextlib.contextmanager
def wait_for_page_load(self, timeout=10):
    self.log.debug("Waiting for page to load at {}.".format(self.driver.current_url))
    old_page = self.find_element_by_tag_name('html')
    yield
    WebDriverWait(self, timeout).until(staleness_of(old_page))
2

Tương tự như

@contextlib.contextmanager
def wait_for_page_load(self, timeout=10):
    self.log.debug("Waiting for page to load at {}.".format(self.driver.current_url))
    old_page = self.find_element_by_tag_name('html')
    yield
    WebDriverWait(self, timeout).until(staleness_of(old_page))
0, nhưng khớp trên một chuỗi một phần.

@contextlib.contextmanager
def wait_for_page_load(self, timeout=10):
    self.log.debug("Waiting for page to load at {}.".format(self.driver.current_url))
    old_page = self.find_element_by_tag_name('html')
    yield
    WebDriverWait(self, timeout).until(staleness_of(old_page))
4

Tìm thẻ HTML bằng thuộc tính tên của chúng. Đây là tiện dụng cho các hình thức HTML.

@contextlib.contextmanager
def wait_for_page_load(self, timeout=10):
    self.log.debug("Waiting for page to load at {}.".format(self.driver.current_url))
    old_page = self.find_element_by_tag_name('html')
    yield
    WebDriverWait(self, timeout).until(staleness_of(old_page))
5

Tìm thẻ HTML bằng tên thẻ của họ.

@contextlib.contextmanager
def wait_for_page_load(self, timeout=10):
    self.log.debug("Waiting for page to load at {}.".format(self.driver.current_url))
    old_page = self.find_element_by_tag_name('html')
    yield
    WebDriverWait(self, timeout).until(staleness_of(old_page))
6

Sử dụng biểu thức XPath ... để chọn các phần tử khớp.

Hướng dẫn how do you check if web page is loaded completely in selenium python? - làm cách nào để kiểm tra xem trang web có được tải hoàn toàn bằng trăn selen không?

Zygd

18.7K38 Huy hiệu vàng70 Huy hiệu bạc89 Huy hiệu đồng38 gold badges70 silver badges89 bronze badges

Đã trả lời ngày 14 tháng 10 năm 2016 lúc 7:19Oct 14, 2016 at 7:19

Hướng dẫn how do you check if web page is loaded completely in selenium python? - làm cách nào để kiểm tra xem trang web có được tải hoàn toàn bằng trăn selen không?

J0ANMMJ0ANMMJ0ANMM

7.2139 Huy hiệu vàng51 Huy hiệu bạc87 Huy hiệu đồng9 gold badges51 silver badges87 bronze badges

3

Từ selenium/webdriver/hỗ trợ/chờ.py

driver = ...
from selenium.webdriver.support.wait import WebDriverWait
element = WebDriverWait(driver, 10).until(
    lambda x: x.find_element_by_id("someId"))

Nam G Vu

31.7K67 Huy hiệu vàng222 Huy hiệu bạc357 Huy hiệu đồng67 gold badges222 silver badges357 bronze badges

Đã trả lời ngày 26 tháng 1 năm 2017 lúc 12:17Jan 26, 2017 at 12:17

Hướng dẫn how do you check if web page is loaded completely in selenium python? - làm cách nào để kiểm tra xem trang web có được tải hoàn toàn bằng trăn selen không?

CarlcarlCarl

2.8362 Huy hiệu vàng30 Huy hiệu bạc47 Huy hiệu Đồng2 gold badges30 silver badges47 bronze badges

0

Bên cạnh một lưu ý phụ, thay vì cuộn xuống 100 lần, bạn có thể kiểm tra xem có thêm sửa đổi cho DOM không (chúng tôi đang ở trong trường hợp dưới cùng của trang được tải xuống Ajax)

def scrollDown(driver, value):
    driver.execute_script("window.scrollBy(0,"+str(value)+")")

# Scroll down the page
def scrollDownAllTheWay(driver):
    old_page = driver.page_source
    while True:
        logging.debug("Scrolling loop")
        for i in range(2):
            scrollDown(driver, 500)
            time.sleep(2)
        new_page = driver.page_source
        if new_page != old_page:
            old_page = new_page
        else:
            break
    return True

Đã trả lời ngày 9 tháng 7 năm 2017 lúc 16:18Jul 9, 2017 at 16:18

Robertspierrerobertspierrerobertspierre

2.5662 Huy hiệu vàng28 Huy hiệu bạc34 Huy hiệu đồng2 gold badges28 silver badges34 bronze badges

6

Bạn đã thử

@contextlib.contextmanager
def wait_for_page_load(self, timeout=10):
    self.log.debug("Waiting for page to load at {}.".format(self.driver.current_url))
    old_page = self.find_element_by_tag_name('html')
    yield
    WebDriverWait(self, timeout).until(staleness_of(old_page))
7 chưa. Nó giống như một cài đặt cho trình điều khiển, vì vậy bạn chỉ gọi nó một lần trong phiên và về cơ bản nó bảo người lái chờ đợi lượng thời gian đã cho cho đến khi mỗi lệnh có thể được thực thi.

driver = webdriver.Chrome()
driver.implicitly_wait(10)

Vì vậy, nếu bạn đặt thời gian chờ là 10 giây, nó sẽ thực hiện lệnh càng sớm càng tốt, hãy chờ 10 giây trước khi bỏ cuộc. Tôi đã sử dụng điều này trong các kịch bản cuộn tương tự vì vậy tôi không hiểu tại sao nó không hoạt động trong trường hợp của bạn. Hy vọng điều này là hữu ích.

Để có thể sửa câu trả lời này, tôi phải thêm văn bản mới. Hãy chắc chắn sử dụng chữ thường 'W' trong

@contextlib.contextmanager
def wait_for_page_load(self, timeout=10):
    self.log.debug("Waiting for page to load at {}.".format(self.driver.current_url))
    old_page = self.find_element_by_tag_name('html')
    yield
    WebDriverWait(self, timeout).until(staleness_of(old_page))
8.

Hướng dẫn how do you check if web page is loaded completely in selenium python? - làm cách nào để kiểm tra xem trang web có được tải hoàn toàn bằng trăn selen không?

Đã trả lời ngày 13 tháng 5 năm 2018 lúc 4:36May 13, 2018 at 4:36

Hướng dẫn how do you check if web page is loaded completely in selenium python? - làm cách nào để kiểm tra xem trang web có được tải hoàn toàn bằng trăn selen không?

seeiespiseeiespiseeiespi

3.3282 Huy hiệu vàng32 Huy hiệu bạc36 Huy hiệu Đồng2 gold badges32 silver badges36 bronze badges

2

Ở đây tôi đã làm nó bằng một hình thức khá đơn giản:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException

browser = webdriver.Firefox()
browser.get("url")
delay = 3 # seconds
try:
    myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))
    print "Page is ready!"
except TimeoutException:
    print "Loading took too much time!"
0

Hướng dẫn how do you check if web page is loaded completely in selenium python? - làm cách nào để kiểm tra xem trang web có được tải hoàn toàn bằng trăn selen không?

Stephen Rauch ♦

45,9K31 Huy hiệu vàng106 Huy hiệu bạc126 Huy hiệu Đồng31 gold badges106 silver badges126 bronze badges

Đã trả lời ngày 27 tháng 10 năm 2018 lúc 15:44Oct 27, 2018 at 15:44

Hướng dẫn how do you check if web page is loaded completely in selenium python? - làm cách nào để kiểm tra xem trang web có được tải hoàn toàn bằng trăn selen không?

Giải pháp cho các trang AJAX liên tục tải dữ liệu. Các phương thức xem trước đã nêu không hoạt động. Thay vào đó, những gì chúng ta có thể làm là lấy trang DOM và băm nó và so sánh các giá trị băm cũ và mới với nhau trong một thời gian delta.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException

browser = webdriver.Firefox()
browser.get("url")
delay = 3 # seconds
try:
    myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))
    print "Page is ready!"
except TimeoutException:
    print "Loading took too much time!"
1

Đã trả lời ngày 22 tháng 7 năm 2020 lúc 20:43Jul 22, 2020 at 20:43

SorobbysorobbySoRobby

2632 Huy hiệu bạc8 Huy hiệu Đồng2 silver badges8 bronze badges

1

Làm thế nào về việc đặt WebDriverWait vào trong khi vòng lặp và bắt các ngoại lệ.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException

browser = webdriver.Firefox()
browser.get("url")
delay = 3 # seconds
try:
    myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))
    print "Page is ready!"
except TimeoutException:
    print "Loading took too much time!"
2

Đã trả lời ngày 8 tháng 5 năm 2017 lúc 6:44May 8, 2017 at 6:44

RaoraoRao

1271 Huy hiệu bạc5 Huy hiệu đồng1 silver badge5 bronze badges

1

Bạn có thể làm điều đó rất đơn giản bởi chức năng này:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException

browser = webdriver.Firefox()
browser.get("url")
delay = 3 # seconds
try:
    myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))
    print "Page is ready!"
except TimeoutException:
    print "Loading took too much time!"
3

Và khi bạn muốn làm gì đó sau khi tải trang hoàn tất, bạn có thể sử dụng:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException

browser = webdriver.Firefox()
browser.get("url")
delay = 3 # seconds
try:
    myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))
    print "Page is ready!"
except TimeoutException:
    print "Loading took too much time!"
4

Đã trả lời ngày 10 tháng 7 năm 2020 lúc 8:23Jul 10, 2020 at 8:23

Hướng dẫn how do you check if web page is loaded completely in selenium python? - làm cách nào để kiểm tra xem trang web có được tải hoàn toàn bằng trăn selen không?

3

Sử dụng mã này trong mã:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException

browser = webdriver.Firefox()
browser.get("url")
delay = 3 # seconds
try:
    myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))
    print "Page is ready!"
except TimeoutException:
    print "Loading took too much time!"
5

Hoặc bạn có thể sử dụng mã này nếu bạn đang tìm kiếm một thẻ cụ thể:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException

browser = webdriver.Firefox()
browser.get("url")
delay = 3 # seconds
try:
    myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))
    print "Page is ready!"
except TimeoutException:
    print "Loading took too much time!"
6

Đã trả lời ngày 16 tháng 8 năm 2020 lúc 13:43Aug 16, 2020 at 13:43

Hướng dẫn how do you check if web page is loaded completely in selenium python? - làm cách nào để kiểm tra xem trang web có được tải hoàn toàn bằng trăn selen không?

Mamalmamalmamal

1.49515 huy hiệu bạc12 Huy hiệu đồng15 silver badges12 bronze badges

Câu trả lời rất tốt ở đây. Ví dụ nhanh về sự chờ đợi cho

@contextlib.contextmanager
def wait_for_page_load(self, timeout=10):
    self.log.debug("Waiting for page to load at {}.".format(self.driver.current_url))
    old_page = self.find_element_by_tag_name('html')
    yield
    WebDriverWait(self, timeout).until(staleness_of(old_page))
6.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException

browser = webdriver.Firefox()
browser.get("url")
delay = 3 # seconds
try:
    myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))
    print "Page is ready!"
except TimeoutException:
    print "Loading took too much time!"
7

Đã trả lời ngày 18 tháng 1 năm 2021 lúc 12:23Jan 18, 2021 at 12:23

FrancoisfrancoisFrancois

10,2K3 Huy hiệu vàng51 Huy hiệu bạc64 Huy hiệu đồng3 gold badges51 silver badges64 bronze badges

Tôi đã đấu tranh một chút để có được công việc này vì điều đó không hiệu quả với tôi như mong đợi. Bất cứ ai vẫn đang vật lộn để có được công việc này, có thể kiểm tra điều này.

Tôi muốn đợi một yếu tố có mặt trên trang web trước khi tiến hành các thao tác của tôi.

Chúng ta có thể sử dụng WebDriverWait (trình điều khiển, 10, 1) .Until (), nhưng việc bắt được cho đến khi () mong đợi một hàm mà nó có thể thực hiện trong một khoảng thời gian chờ được cung cấp (trong trường hợp của chúng tôi 10) trong mỗi 1 giây. Vì vậy, giữ nó như dưới đây làm việc cho tôi.until() expects a function which it can execute for a period of timeout provided(in our case its 10) for every 1 sec. so keeping it like below worked for me.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException

browser = webdriver.Firefox()
browser.get("url")
delay = 3 # seconds
try:
    myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))
    print "Page is ready!"
except TimeoutException:
    print "Loading took too much time!"
8

Đây là những gì cho đến khi () làm đằng sau hiện trườnguntil() do behind the scene

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException

browser = webdriver.Firefox()
browser.get("url")
delay = 3 # seconds
try:
    myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))
    print "Page is ready!"
except TimeoutException:
    print "Loading took too much time!"
9

Đã trả lời ngày 6 tháng 9 năm 2021 lúc 7:05Sep 6, 2021 at 7:05

Selenium không thể phát hiện khi trang được tải đầy đủ hay không, nhưng JavaScript có thể. Tôi đề nghị bạn thử điều này.

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()
driver.get('url')
timeout = 5
try:
    element_present = EC.presence_of_element_located((By.ID, 'element_id'))
    WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
    print "Timed out waiting for page to load"
0

Điều này sẽ thực thi mã JavaScript thay vì sử dụng Python, vì JavaScript có thể phát hiện khi trang được tải đầy đủ, nó sẽ hiển thị 'Hoàn thành'. Mã này có nghĩa là trong 100 giây, hãy tiếp tục thử tài liệu.ReadyState cho đến khi hoàn thành hiển thị.

Đã trả lời ngày 19 tháng 7 lúc 10:28Jul 19 at 10:28

Nếu bạn đang cố gắng cuộn và tìm tất cả các mục trên một trang. Bạn có thể xem xét sử dụng những điều sau đây. Đây là sự kết hợp của một vài phương pháp được đề cập bởi những người khác ở đây. Và nó đã làm công việc cho tôi:

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()
driver.get('url')
timeout = 5
try:
    element_present = EC.presence_of_element_located((By.ID, 'element_id'))
    WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
    print "Timed out waiting for page to load"
1

Đã trả lời ngày 30 tháng 11 năm 2021 lúc 20:18Nov 30, 2021 at 20:18

Làm thế nào để tôi biết khi một trang đã tải xong trong selenium?

Chúng ta có thể khiến Selenium nhận ra rằng một trang được tải. Chúng ta có thể đặt sự chờ đợi ngầm cho mục đích này. Nó sẽ khiến người lái chờ đợi một lượng thời gian cụ thể để có sẵn một yếu tố sau khi tải trang.set the implicit wait for this purpose. It shall make the driver to wait for a specific amount of time for an element to be available after page loaded.

Làm thế nào để Python xử lý tải trang trong selenium?

Có ba cách khác nhau để thực hiện Selenium Chờ trong Python cho trang để tải: Chờ đợi rõ ràng ...
Trình điều khiển - Đây là ví dụ của webdriver bạn đang sử dụng để thực hiện thử nghiệm ứng dụng của mình.....
Thời gian chờ - Nó đề cập đến số giây trước khi sự chờ đợi này không thành công và ném một ngoại lệ ..

Phương pháp nào sẽ đợi cho đến khi trang được tải đầy đủ?

Chúng ta có thể đợi cho đến khi trang được tải hoàn toàn trong selenium webdriver bằng cách sử dụng JavaScript Executor.Selenium có thể chạy các lệnh JavaScript với sự trợ giúp của phương thức thực thi.

Làm cách nào để tải lại một trang web bằng Selenium Python?

Chúng tôi có thể làm mới một trang web bằng Selenium WebDriver trong Python.Điều này có thể được thực hiện với sự trợ giúp của phương pháp làm mới.Trước hết, chúng tôi phải khởi chạy ứng dụng với phương thức Get.Khi một trang web được tải hoàn toàn, sau đó chúng ta có thể làm mới trang với sự trợ giúp của phương thức làm mới.with the help of the refresh method. First of all, we have to launch the application with the get method. Once a web page is loaded completely, we can then refresh the page with the help of the refresh method.