我需要從這個網址中洗掉一些資料:

我無法單擊 onclick 元素,它應該讓我從一個選項卡切換到另一個選項卡。
這里是 3 個 onclick 元素之一的 html 代碼:

3 個 onclick 元素的不同之處在于末尾的數字:
#COUPE1:
return sendRequest(5,'/definition/coupe//0');
#COUPE2:
return sendRequest(5,'/definition/coupe//1');
#COUPER:
return sendRequest(5,'/definition/coupe//2');
我試圖通過鏈接文本、部分鏈接文本、xpath 和 css 選擇器找到它們。
我關注了這個執行緒: Python Selenium:如何點擊“onclick”元素?
還可以嘗試 contains 和 text() 方法。
沒有成功。
uj5u.com熱心網友回復:
有幾種方法可以做到這一點。我選擇了我所做的方法,因為頁面重新加載導致元素變得陳舊。這是您在 mr_mooo_cow 的回答中會發現的問題。
#Get the URL
driver.get("https://www.cnrtl.fr/definition/coupe")
#Find the parent element of the tabs
tabs = driver.find_element(By.ID, 'vtoolbar')
#Get all the list items under the parent (tabs)
lis = tabs.find_elements(By.TAG_NAME, 'li')
#loop over them (skipping the first tab, because that's already loaded)
for i in range(1, len(lis)):
#Execute the same JS as the page would on click, using the index of the loop
driver.execute_script(f"sendRequest(5,'/definition/coupe//{i}');")
#Sleep to visualise the clicking
time.sleep(3)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/473591.html
標籤:python-3.x 硒 路径 点击 点击
