Cannot Read Property 'click' Of Undefined While Using Java Script Executor In Selenium
Solution 1:
This error message...
Cannot read property 'click' of undefined
...implies that the click()
method can't be executed as the WebElement haven't completely rendered within the DOM Tree and the element is still in undefined state.
Details
As you mentioned that the "Java Script is working in console" that implies the JavaScript is perfecto. The main issue is the element haven't rendered completely within the HTML DOM.
Solution
As a solution, you need to induce you need to induce WebDriverWait for the visibilityOfAllElementsLocatedBy()
and you can use either of the following Locator Strategies:
Using
cssSelector
:new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector(".button.button--new-resource"))); ((JavascriptExecutor) driver).executeScript("var x= document.getElementsByClassName('button button--new-resource')[0];"+"x.click();");
Using
xpath
:new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//*[@class='button button--new-resource']"))); ((JavascriptExecutor) driver).executeScript("var x= document.getElementsByClassName('button button--new-resource')[0];"+"x.click();");
Best practices
However, as per best practices to click on the 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, "a.button.button--new-resource[href='/admin/certificate_types/new']>img[alt='Icon add user']"))).click()
Using
XPATH
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='button button--new-resource' and @href='/admin/certificate_types/new']/img[@alt='Icon add user']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.uiimportWebDriverWaitfrom selenium.webdriver.common.byimportByfrom selenium.webdriver.supportimport expected_conditions asEC
Solution 2:
This is the wrong way to pass element into the JSExecutor, you need to combine it with FindElement call. In your case, the script might be executed before the element can be found, resulting 'undefined' error.
// find the elementvar element = new WebDriverWait(driver, 20).until(ExpectedConditions.ElementToBeClickable(By.xpath("//*[@class='button button--new-resource']")));
// execute the click on the element you have found
((JavascriptExecutor)driver).ExecuteScript("arguments[0].click();", element);
Solution 3:
the problem is in quotation marks
js.executeScript("document.getElementsByClassName('button button--new-resource')[0].click();");
should do the job
if there is only one button
<a class="button button--new-resource" href="/admin/certificate_types/new">
use document.getElementByClassName
and do not use index:
js.executeScript("document.getElementByClassName('button button--new-resource').click();");
Solution 4:
your locator is wrong , its a multi class element use '.' instead of space
js.executeScript("var x= document.getElementsByClassName('button.button--new-resource')[0];"+"x.click();");
Solution 5:
Why do you use js executor? Try
WebElementelement= driver.findElement(By.className(CLASS_NAME));
element.click();
Post a Comment for "Cannot Read Property 'click' Of Undefined While Using Java Script Executor In Selenium"