kingliiis
kingliiis

Reputation: 41

How to click on image using selenium

I'm trying to click on button that contains this code :

<div style="text-align:center" id="plus_res">
<img src="https://www.tunlancer.com/theme/images/plus_resv2.png" align="plus d'elements" title="plus d'elements" style="cursor:pointer" onclick="plus_resultat()">
</div>

i've tried :

image = driver.find_element_by_xpath("//img[contains(@src,'https://www.tunlancer.com/theme/images/plus_resv2.png')]")

driver.execute_script("arguments[0].click();", image)
driver.find_element_by_id('plus_res').click()
driver.find_element_by_xpath('//*[@id="plus_res"]/img').click()

But it's showing the NoSuchElementException . How can i fix it please?

Upvotes: 2

Views: 1318

Answers (2)

Unused hero
Unused hero

Reputation: 307

Possibly you have to add delay.
Something like this should work :

time.sleep(5)
driver.find_element_by_css_selector('img[src="https://www.tunlancer.com/theme/images/plus_resv2.png"]').click()

Upvotes: 1

Prophet
Prophet

Reputation: 33351

I guess you are missing a delay / wait before accessing that element.
Please try this:

wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, '//img[@onclick="plus_resultat()"]'))).click()

You will need the following imports:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Also possibly the element is inside an iframe.

Upvotes: 0

Related Questions