Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read property 'defaultView' of undefined

Tags:

selenium

<a href="/lightning/n/******__Country" title="Country" tabindex="0" draggable="false" aria-describedby="operationId-17" class="slds-context-bar__label-action dndItem">
  <span class="slds-truncate">Country</span></a>

I got the xpath as

WebElement tabName = driver.findElement(By.xpath("//a[contains(@href,'Country')]"));

I need to click the Country link

I have tried the following options but none work

 1) driver.findElement(By.xpath("//a[contains(@href,'Country') and @title='Country']")).click();

 2)     Actions actions = new Actions((WebDriver) driver.getWebDriver());
                actions.moveToElement(tabName).click().perform();

 3)         ((JavascriptExecutor) driver).executeScript("arguments[0].click();", tabName);
                waitForSeconds(5);

I am getting invocation of target exception

org.openqa.selenium.WebDriverException: javascript error: Cannot read property 'defaultView' of undefined

How to click the href link?

like image 214
Sindiya Avatar asked Dec 22 '25 00:12

Sindiya


2 Answers

Try click on its parent/ancestor instead. For example, if you had

//a[text()='link of your text']

and you get the javascript error, try:

//a[text()='link of your text']/parent::*
like image 166
Vivian Yung Avatar answered Dec 31 '25 19:12

Vivian Yung


Try the following code - it works against Salesforce Lightning UI screens:

      WebElement element = driver.findElement(By.xpath("your xpath"));
      JavascriptExecutor executor = (JavascriptExecutor)driver;
      executor.executeScript("arguments[0].click();", element);

Sharing it after testing it and found to be working.

like image 20
Salmaan Avatar answered Dec 31 '25 19:12

Salmaan