<img id="showlink" x-onclick="changeLink()" src="https://eductin.com/wp-content/uploads/2021/06/Download.png">
I need to click on this element but the click function of selenium is not working. Here is my code.
wd.find_element(value="showlink").click()
Have no error but it is not working.
CodePudding user response:
This element can be located based on several attributes: id
, x-onclick
, src
etc.
For example
wd.find_element(By.ID,'showlink')
But it is always preferable to use expected conditions webdriver waits, as following:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(wd, 20).until(EC.element_to_be_clickable((By.ID, "showlink"))).click()
CodePudding user response:
Given the HTML:
<img id="showlink" x-onclick="changeLink()" src="https://eductin.com/wp-content/uploads/2021/06/Download.png">
To click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "img.spoint#showlink[x-onclick^='changeLink']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//img[@class='spoint' and @id='showlink'][starts-with(@x-onclick, 'changeLink')]"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC