Home > Back-end >  Python Selenium Click On Hover Button
Python Selenium Click On Hover Button

Time:01-25

I am trying to click a button to initiate a file download. The button has hover-over functionality from what I have seen. I have tried clicking via all find_element_by paths. HTML.

HTML chunk of the page,

<button  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 ><span>Download</span></span></button>

The button's class before hovering is,

"btn-retrieve ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"

The button's class when hovering is,

"btn-retrieve ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-state-hover"

Selecting the button via the developer window and copying over paths from

<span>Download</span>==$0 

I get the following,

CSS path,

#sub_1\:listPgFrm\:j_idt6461\:downloadBtn > span > span

XPath,

//*[@id="sub_1:listPgFrm:j_idt6461:downloadBtn"]/span/span

Code I have tried,

dwnd = driver.find_element_by_xpath("//*[@id='sub_1:listPgFrm:j_idt6461:downloadBtn']/span")
dwnd.click()

And,

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()

Word document of the page's entire HTML code.

CodePudding user response:

You can use the text-based locator:

//button[.='Download']

Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

and you can click on it:

Code trial 1:

time.sleep(5)
driver.find_element(By.XPATH, "//button[.='Download']").click()

Code trial 2:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[.='Download']"))).click()

Code trial 3:

time.sleep(5)
button = driver.find_element(By.XPATH, "//button[.='Download']")
driver.execute_script("arguments[0].click();", button)

Code trial 4:

time.sleep(5)
button = driver.find_element(By.XPATH, "//button[.='Download']")
ActionChains(driver).move_to_element(button).click().perform()

Imports:

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
  •  Tags:  
  • Related