搜尋
首頁後端開發Python教學如何使用Python從Bootstrap選項卡點擊href連結?

如何使用Python從Bootstrap選項卡點擊href連結?

Sep 01, 2023 pm 04:21 PM
pythonbootstraphref

Bootstrap 是流行的 HTML、CSS、JavaScript 框架,可協助我們開發響應式、行動優先的前端 Web 應用程式。它提供表單、排版、導航、按鈕和其他介面組件的設計範本。 Python 是操作網頁內容的最佳語言。

硒庫

如果我們需要使用 Python 程式設計來點擊鏈接,我們應該使用 selenium 函式庫。它是最受歡迎的開源自動化測試工具,它使我們能夠使網頁瀏覽器自動化。

Selenium 主要用於自動化 Web 應用程式的測試目的,也用於其他目的,例如自動化重複任務和網頁抓取。它支援Python、Java、C和Ruby等程式語言。可用於測試Google、Mozilla Fire Fox、safari等網頁瀏覽器。

應遵循的步驟

以下是使用 selenium 從引導標籤自動開啟給定 href 連結的步驟。

  • 安裝selenium函式庫:首先,我們要在python環境中安裝selenium函式庫。以下是cod

pip install Selenium

如果安裝成功,我們將得到以下輸出 -

Collecting Selenium
  Downloading selenium-4.8.3-py3-none-any.whl (6.5 MB)
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Installing collected packages: outcome, h11, exceptiongroup, async-generator, wsproto, trio, trio-websocket, Selenium
Successfully installed Selenium-4.8.3 async-generator-1.10 exceptiongroup-1.1.1 h11-0.14.0 outcome-1.2.0 trio-0.22.0 trio-websocket-0.10.2 wsproto-1.2.0
Note: you may need to restart the kernel to use updated packages.
  • 匯入 Web 驅動程式 - selenium 套件用於自動化 Python 中的 Web 瀏覽器互動。支援多種瀏覽器/驅動程式(Firefox、Chrome、Internet Explorer)以及遠端協定。

從 Selenium 庫導入 webdriver 套件。

from selenium import webdriver
  • 在此步驟中,我們將網站的驅動程式與 webdriver 套件連結

#
web_driver = webdriver.Chrome("D://Myspace/chromedriver.exe")
  • 接下來,我們將透過指派網站鏈接,使用 webdriver 套件的 get() 函數開啟 href 連結。

web_driver.get("https://www.Tutorialspoint.com/")

讓我們將上述所有步驟組合在一起並查看輸出。

from selenium import webdriver
web_driver = webdriver.Chrome("D://Myspace/chromedriver.exe")
web_driver.get("https://www.Tutorialspoint.com/")
print("The website link opened")

輸出

下面是上面程式碼的輸出,當我們執行程式時,指定的連結將會被開啟。

如何使用Python從Bootstrap選項卡點擊href連結?

範例

以下是使用 python 從引導標籤點擊 href 連結的另一個範例。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = "https://Tutorialspoint.com"
driver_path = "path/to/webdriver"
driver = webdriver.Chrome(driver_path)
driver.get(url)
tab_link = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CSS_SELECTOR, 'a[data-toggle="tab"][href="#tab-1"]'))
)
tab_link.click()
WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "tab-1"))
)
link_url = driver.current_url
driver.get(link_url)
print(driver.page_source)
driver.quit()

輸出

上述程式碼的輸出如下。

如何使用Python從Bootstrap選項卡點擊href連結?

以上是如何使用Python從Bootstrap選項卡點擊href連結?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:tutorialspoint。如有侵權,請聯絡admin@php.cn刪除
Python腳本可能無法在UNIX上執行的一些常見原因是什麼?Python腳本可能無法在UNIX上執行的一些常見原因是什麼?Apr 28, 2025 am 12:18 AM

Python腳本在Unix系統上無法運行的原因包括:1)權限不足,使用chmod xyour_script.py賦予執行權限;2)Shebang行錯誤或缺失,應使用#!/usr/bin/envpython;3)環境變量設置不當,可打印os.environ調試;4)使用錯誤的Python版本,可在Shebang行或命令行指定版本;5)依賴問題,使用虛擬環境隔離依賴;6)語法錯誤,使用python-mpy_compileyour_script.py檢測。

舉一個場景的示例,其中使用Python數組比使用列表更合適。舉一個場景的示例,其中使用Python數組比使用列表更合適。Apr 28, 2025 am 12:15 AM

使用Python數組比列表更適合處理大量數值數據。 1)數組更節省內存,2)數組對數值運算更快,3)數組強制類型一致性,4)數組與C語言數組兼容,但在靈活性和便捷性上不如列表。

在Python中使用列表與數組的性能含義是什麼?在Python中使用列表與數組的性能含義是什麼?Apr 28, 2025 am 12:10 AM

列表列表更好的forflexibility andmixDatatatypes,何時出色的Sumerical Computitation sand larged數據集。 1)不可使用的列表xbilese xibility xibility xibility xibility xibility xibility xibility xibility xibility xibility xibles and comply offrequent elementChanges.2)

Numpy如何處理大型數組的內存管理?Numpy如何處理大型數組的內存管理?Apr 28, 2025 am 12:07 AM

numpymanagesmemoryforlargearraysefefticefticefipedlyuseviews,副本和內存模擬文件.1)viewsAllowSinglicingWithOutCopying,直接modifytheoriginalArray.2)copiesCanbecopy canbecreatedwitheDedwithTheceDwithThecevithThece()methodervingdata.3)metservingdata.3)memore memore-mappingfileShessandAstaStaStstbassbassbassbassbassbassbassbassbassbassbb

哪個需要導入模塊:列表或數組?哪個需要導入模塊:列表或數組?Apr 28, 2025 am 12:06 AM

Listsinpythondonotrequireimportingamodule,helilearraysfomthearraymoduledoneedanimport.1)列表列表,列表,多功能和canholdMixedDatatatepes.2)arraysaremoremoremoremoremoremoremoremoremoremoremoremoremoremoremoremoremeremeremeremericdatabuteffeftlessdatabutlessdatabutlessfiblesible suriplyElsilesteletselementEltecteSemeTemeSemeSemeSemeTypysemeTypysemeTysemeTypysemeTypepe。

可以在Python數組中存儲哪些數據類型?可以在Python數組中存儲哪些數據類型?Apr 27, 2025 am 12:11 AM

pythonlistscanStoryDatatepe,ArrayModulearRaysStoreOneType,and numpyArraySareSareAraysareSareAraysareSareComputations.1)列出sareversArversAtileButlessMemory-Felide.2)arraymoduleareareMogeMogeNareSaremogeNormogeNoreSoustAta.3)

如果您嘗試將錯誤的數據類型的值存儲在Python數組中,該怎麼辦?如果您嘗試將錯誤的數據類型的值存儲在Python數組中,該怎麼辦?Apr 27, 2025 am 12:10 AM

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

Python標準庫的哪一部分是:列表或數組?Python標準庫的哪一部分是:列表或數組?Apr 27, 2025 am 12:03 AM

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能