1

The "To" Buttom is unable to take keys from selenium webdriver. In output it shows Unable to locate element. "To" is in iframe and I have used I frame but then also it not working.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class mail {
    public static void main(String[] args) throws InterruptedException  {

        System.setProperty("webdriver.gecko.driver", "D:\mozilla   driver\geckodriver.exe");
        WebDriver driver=new FirefoxDriver();
        driver.get("https://www.mail.com/int/");
        driver.findElement(By.xpath(".//*[@id='login-button']")).click();
        driver.findElement(By.xpath(".//*[@id='login-email']")).sendKeys("rahulrahulxyz@mail.com");
        driver.findElement(By.xpath(".//*[@id='login-password']")).sendKeys("incredible");
        driver.findElement(By.xpath(".//*[@id='login-form']/button")).click();

        driver.switchTo().frame("thirdPartyFrame_home");
        driver.findElement(By.linkText("Compose E-mail")).click();
        Thread.sleep(5000);

        driver.switchTo().frame("thirdPartyFrame_mail");     // **here is error**
        driver.findElement(By.xpath(".//*[@id='idbd']/div[2]/div[1]/div[1]/div[2]/div/div/ul/li/input")).sendKeys("abcde@mail.com");
    }
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • if it is hierarchical order in frames, then you need to switch to the parent frame -> then child frame. See if this answer helps you https://stackoverflow.com/a/40759300/2575259 – Naveen Kumar R B Apr 04 '18 at 11:15

1 Answers1

0

To send the Emailid through sendKeys() method you have to switch back to the defaultContent first then switch again to the intended frame with WebDriverWait and finally induce WebDriverWait for the To field to be interactable and then send the Emailid as follows :

  • Code Block :

    driver.switchTo().defaultContent();
    new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("thirdPartyFrame_mail")));
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='compose-header_item compose-header_to mailobjectpanel-objectivation mailobjectpanel-textfield js-component mailobjectpanel']//div[@class='select2-container select2-container-multi js-select2']/ul/li/input"))).sendKeys("abcde@mail.com");
    
  • Snapshot :

Email-ID

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I understand all the codes except xpath. Can you explain me why you write such a xpath and why firebug xpath is not working. Using this type of xpath will waste lots of time for automating whole page. Is their any way to take out this type of xpath very easily. . https://drive.google.com/open?id=1gdabzuXiaqonzI0ulnP8nkD6P53cAMKE – shovit magar Apr 04 '18 at 14:32
  • The xpath I have written simply mocks the dom and is a _logical xpath_. Firebug will provide you _absolute xpaths_ only. So you have to gradually move from using _absolute xpaths_ to _logical xpath_ which will provide the much needed stability. – undetected Selenium Apr 04 '18 at 14:37
  • Why not use a simple XPath like: //*[@id='idbd']//input – Frank Apr 04 '18 at 14:44
  • 1
    @DebanjanB Thank You Very Much. My problem is solved now. – shovit magar Apr 04 '18 at 14:44
  • @Frank There are multiple elements. To make the xpath unique I had to. – undetected Selenium Apr 04 '18 at 14:47
  • @DebanjanB you're right, I overlooked it. In that case '(//*[@id='idbd']//input)[1]' looks more stable to me. – Frank Apr 04 '18 at 14:52
  • @Frank But I won't resort to an _index_ until I run out of options :) – undetected Selenium Apr 04 '18 at 14:54
  • Do I have to write defaultContent every time whenever I need to send key in this iframe "mail". Like for sending keys in "subject" do I have to write first 2 lines of your code again. – shovit magar Apr 04 '18 at 16:21
  • @shovitmagar Whenever you need to change switch to a new frame always go back to the TopLevel Context (defaultContent) and then switch to the intended frame. – undetected Selenium Apr 04 '18 at 16:23
  • @Debanjan Right now I am run the same above code without making any changes. Their are 3 diff error are coming running at 3 diff times the same code above. Plz help me regarding this why this diff diff error are coming. Check this link https://drive.google.com/open?id=1deIfD3VsoUqFeGbbtUYPC8Lr8Mi-3F0Y – shovit magar Apr 04 '18 at 19:01
  • @shovitmagar The errors are due to mismatch in compatibility between the binaries you are using. Upgrade your Selenium client, WebDriver variant and the WebClient variant to current levels. – undetected Selenium Apr 04 '18 at 19:09