Chúng ta có thể truy cập mã nguồn HTML bằng Selenium webdriver. Chúng tôi có thể nhờ sự trợ giúp của page_source và in giá trị thu được từ nó trong bảng điều khiển.
Cú pháp
src = driver.page_source
Chúng ta cũng có thể truy cập mã nguồn HTML với sự trợ giúp của các lệnh Javascript trong Selenium. Chúng tôi sẽ nhận sự trợ giúp của execute_script và chuyển lệnh return document.body.innerHTML như một tham số cho phương thức.
Cú pháp
h = driver.execute_script("return document.body.innerHTML;") Ví dụ
Triển khai mã.
from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.implicitly_wait(0.5)
driver.get("https://www.tutorialspoint.com/index.htm")
# access HTML source code with page_source method
s = driver.page_source
print(s) Triển khai mã với Trình thực thi Javascript.
from selenium import webdriver
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.implicitly_wait(0.5)
driver.get("https://www.tutorialspoint.com/index.htm")
# access HTML source code with Javascript command
h = driver.execute_script("return document.body.innerHTML")
print(h)