Home > Back-end >  Selenium: No such element exception is getting on this http://www.salesfoce.com/ even thought that e
Selenium: No such element exception is getting on this http://www.salesfoce.com/ even thought that e

Time:04-27

I am trying to find an element By.cssSelector("#e1"). On this website : This button

From java, I tried By.xPath and By.cssSelector but every time I am getting No such element exception. I also used Implicit and explicit wait still getting same exception.

But if I find using inspect element than it highlights the element. enter image description here

CodePudding user response:

The element Salesforce is under iframe you need to switch to iframe first in order to access the element.

Use WebdriverWait() and wait for frameToBeAvailableAndSwitchToIt() and use following locator to identify.

Use WebdriverWait() and wait for elementToBeClickable() and use following locator to identify.

driver.get("http://ww1.salesfoce.com/")
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("#rs>iframe")));

new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Salesforce']"))).click();

CodePudding user response:

The element with the text as Salesforce is within an <iframe>

salesforce_iframe


Solution

To click on the element with text as Salesforce within the website you have to:

  • Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
  • Induce WebDriverWait for the desired elementToBeClickable.
  • You can use either of the following Locator Strategies:
    • Using xpath:

      driver.get("http://ww1.salesfoce.com/");
      new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@src, 'https://www.google.com/afs/ads?adtest')]")));
      new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Salesforce']"))).click();
      
  • Related