【问题标题】:Python Selenium Click On Hover ButtonPython Selenium 单击悬停按钮
【发布时间】:2022-01-24 15:08:21
【问题描述】:

我正在尝试单击按钮来启动文件下载。该按钮具有我所看到的悬停功能。我试过点击全部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()

【问题讨论】:

  • 可以提供网址吗?
  • 你至少可以分享页面 HTML 吗?不是单个元素而是整个页面?
  • 我明白了。我不确定我能做些什么。也许其他人可以

标签: python selenium selenium-webdriver web-scraping selenium-chromedriver


【解决方案1】:

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

//button[.='Download']

如果我们在HTML DOM 中有唯一条目,请检查dev tools(谷歌浏览器)。

检查步骤:

Press F12 in Chrome -> 转到element 部分 -> 做一个CTRL + F -> 然后粘贴xpath 看看,如果你想要的element 用@ 得到高亮 987654329@匹配节点。

你可以点击它:

代码试用 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

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 2021-03-18
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    • 2021-01-31
    • 2020-07-30
    相关资源
    最近更新 更多