Python Selenium单击悬停按钮



我正试图单击一个按钮来启动文件下载。该按钮具有悬停在我所看到的功能之上。我已尝试通过所有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({&quot;source&quot;:&quot;sub_1:listPgFrm:j_idt6461:downloadBtn&quot;,&quot;execute&quot;:'@all',&quot;render&quot;:'@all',&quot;event&quot;:&quot;activate&quot;,&quot;onstart&quot;: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()

您可以使用基于文本的定位器:

//button[.='Download']

如果我们在HTML DOM中是否有唯一条目,请检查dev tools(Google chrome(。

检查步骤:

Press F12 in Chrome->转到element部分->进行CTRL + F->然后粘贴xpath,看看您想要的element是否通过1/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

最新更新