我正在嘗試單擊一個按鈕來啟動檔案下載。該按鈕具有我所看到的懸停功能。我試過點擊全部find_element_by paths。
頁面的 HTML 塊,
<button class="btn-retrieve ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" id="sub_1:listPgFrm:j_idt6461:downloadBtn" name="sub_1:listPgFrm:j_idt6461:downloadBtn" onclick="new ice.ace.DataExporter('sub_1:listPgFrm:j_idt6461:downloadBtn', function() { ice.ace.ab(ice.ace.extendAjaxArgs({"source":"sub_1:listPgFrm:j_idt6461:downloadBtn","execute":'@all',"render":'@all',"event":"activate","onstart":function(cfg){showProcessingMessage('Downloading'); return true;;}}, {node:this})); });return false;" style="margin-top: -4px;" role="button" aria-disabled="false"><span class="ui-button-text"><span>Download</span></span></button>
懸停前按鈕的類是,
"btn-retrieve ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
懸停時按鈕的類是,
"btn-retrieve ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-state-hover"
通過開發人員視窗選擇按鈕并復制路徑
<span>Download</span>==$0
我得到以下資訊,
CSS 路徑,
#sub_1\:listPgFrm\:j_idt6461\:downloadBtn > span > span
XPath,
//*[@id="sub_1:listPgFrm:j_idt6461:downloadBtn"]/span/span
我試過的代碼,
dwnd = driver.find_element_by_xpath("//*[@id='sub_1:listPgFrm:j_idt6461:downloadBtn']/span")
dwnd.click()
和,
button = driver.find_element_by_css_selector("#sub_1\:listPgFrm\:j_idt6461\:downloadBtn")
download = driver.find_element_by_link_text("Download")
hover = ActionChains(driver).move_to_element(button).move_to_element(download)
hover.click().build().perform()
uj5u.com熱心網友回復:
您可以使用基于文本的定位器:
//button[.='Download']
dev tools如果我們有獨特的條目,請檢查(谷歌瀏覽器)HTML DOM。
檢查步驟:
Press F12 in Chrome-> 轉到element部分 -> 執行CTRL F-> 然后粘貼xpath并查看,如果您想要的element是否使用匹配節點突出顯示1/1。
你可以點擊它:
代碼試用一:
time.sleep(5)
driver.find_element(By.XPATH, "//button[.='Download']").click()
代碼試用2:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[.='Download']"))).click()
代碼試用3:
time.sleep(5)
button = driver.find_element(By.XPATH, "//button[.='Download']")
driver.execute_script("arguments[0].click();", button)
代碼試用4:
time.sleep(5)
button = driver.find_element(By.XPATH, "//button[.='Download']")
ActionChains(driver).move_to_element(button).click().perform()
進口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/420600.html
標籤:
上一篇:FileNotFoundError:[Errno2]Nosuchfileordirectory:'/content/drive/MyDrive/Santillana/geckodriver&
