Python Selenium 单击悬停按钮
Python Selenium Click On Hover Button
我正在尝试单击按钮来启动文件 下载。根据我所见,该按钮具有悬停功能。我试过点击所有 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>
悬停前按钮的 class 是,
"btn-retrieve ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
悬停时按钮的class是,
"btn-retrieve ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-state-hover"
通过开发者选择按钮 window 并从
复制路径
<span>Download</span>==[=14=]
我得到以下信息,
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()
您可以使用 text-based 定位器:
//button[.='Download']
请检查 dev tools
(Google chrome) 我们是否在 HTML DOM
中有 unique 条目。
检查步骤:
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
我正在尝试单击按钮来启动文件 下载。根据我所见,该按钮具有悬停功能。我试过点击所有 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>
悬停前按钮的 class 是,
"btn-retrieve ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
悬停时按钮的class是,
"btn-retrieve ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-state-hover"
通过开发者选择按钮 window 并从
复制路径<span>Download</span>==[=14=]
我得到以下信息,
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()
您可以使用 text-based 定位器:
//button[.='Download']
请检查 dev tools
(Google chrome) 我们是否在 HTML DOM
中有 unique 条目。
检查步骤:
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