How to click an image with Selenium in Python?

Viewed 70

I'm trying to click this button, but the button is actually an image which doesn't have an ID. I've tried using the Xpath by doing

WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, "/html/body/table/tbody/tr[1]/td/a"))
).click()

but this wasn't able to find the element. Any help would be great! Here's the relevant HTML

<a class="navcontent" href=f5-h-$$/MSL/jsp/openGimPage.jsp?gimEnv=GIM&amp;ep=GIMV" target="blank>
<img onclick="F5_r2u();F5_Event_common(event);try{return(eval(F5_Invoke_eval_event(null,F5_jsBody(function(){parent.trackUserActivity(this,'o','Portlet : Incident Management '+(this.src).split('/images/')[1].split('.')[0]);
<p id="text" style="top: 7px">View</p></a>

Edit: Here is the remainder of the HTML Remainder of HTML

3 Answers

Found the answer. As KunduK pointed out, it was within an iFrame. I had to use the following to select the iFrame, then click the button within.

driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
driver.find_element_by_css_selector("body > table > tbody > tr:nth-child(1) > td > a")

Try following xpath to identify the element. Use element_to_be_clickable() instead presence_of_element_located()

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[./p[text()='View']]"))).click()

Thank you for your suggestion. I also had this question,and solved by Tyler's way. But I used code below to solved the problem.

driver.switch_to.frame(browser.find_element_by_tag_name("iframe"))    
driver.find_element(by=By.XPATH, value="XPATH")    
Related