Home > Back-end >  Click On Element That Has 'x-onclick' with Python Selenium
Click On Element That Has 'x-onclick' with Python Selenium

Time:09-17

I am trying to use selenium to click on an element with x-onclick property not onclick. Screenshot of it

I am using this element's xPath to click on it. These are the methods I have tried:

driver.execute_script("arguments[0].click();", element)

element.click()

but these don't work. I would love if someone can tell me a solution.

CodePudding user response:

When you tried this

driver.execute_script("arguments[0].click();", element)

element is a web element. I do not know if you have defined it or not. If not defined then you must have got compile time error.

Anyway this looks to me an angular based application. So I would try with below code trials :

There are 4 ways to click in Selenium.

I will use this xpath

//a[@id='generater' and @x-onclick]

Code trial 1 :

time.sleep(5)
driver.find_element_by_xpath("//a[@id='generater' and @x-onclick]").click()

Code trial 2 :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@id='generater' and @x-onclick]"))).click()

Code trial 3 :

time.sleep(5)
button = driver.find_element_by_xpath("//a[@id='generater' and @x-onclick]")
driver.execute_script("arguments[0].click();", button)

Code trial 4 :

time.sleep(5)
button = driver.find_element_by_xpath("//a[@id='generater' and @x-onclick]")
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

CodePudding user response:

Have you tried to click by creating xpath by using tag 'a' and 'id' by using simple selenium webdriver click method.

    ele = driver.find_element_by_xpath("//a[@id='generater']")
    ele.click()

    or

    driver.find_element_by_xpath("//a[@id='generater']").click()  



if above xpath is unique then it should work else use javascript for clicking on above element 'ele'.
  • Related