Why would sending a string with HTML tags using WebDriver's SendKeys() cause the browser to navigate to random pages?

StackOverflow https://stackoverflow.com/questions/16418057

Question

I am trying to send a string variable containing HTML code to a textbox located inside a frame. The HTML code looks like this:

<iframe id="rte" class="rteIfm" frameborder="0" contenteditable="" title="Description">
<html>
<head>
</head>
<body role="textbox" aria-multiline="true">
</body>
</html>
</iframe>

I have tried two things... Firstly, I tried switching frames and using the x-path that firebug gave me to send the keys:

 driver.SwitchTo().Frame(driver.FindElement(By.Id("rte")));
 driver.FindElement(By.XPath("/html/body")).SendKeys(myStringContainingHTML);

Secondly, I tried sending the keys to the element with the ID the same as the frame:

 driver.FindElement(By.Id("rte")).SendKeys(myStringContainingHTML);

In both cases the same thing happened: at first the string (containing HTML code) began to be typed into the textbox as expected. Then after about one tag was typed the browser started to navigate to different pages. I went to google and started typing in the search box and then searching for chunks of HTML code that were in the string.

Seems very strange to me, where did I go wrong?

Was it helpful?

Solution

I still don't know why this is happening but the workaround I'm using its to programmatically copy the string to clipboard and past it with WebDriver SendKeys() which is normally as simple as:

Clipboard.SetText(myStringContainingHTML);
driver.FindElement(By.Id("myTxtBoxId")).SendKeys(OpenQA.Selenium.Keys.LeftControl + "v"); 

But actually I tried to do it while multithreading and got the error:

 "Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it."

So I had to do this workaround just to get it in clipboard:

class MyAsyncClass
{

static IWebDriver driver;

public static void MyAsyncMethod()
{
 FirefoxProfile myProfile = new FirefoxProfile();
 driver = new FirefoxDriver(myProfile);
 driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(20));

 STAClipBoard(myStringWithHtmlCode);
 driver.FindElement(By.Id("myTxtBoxId")).SendKeys(OpenQA.Selenium.Keys.LeftControl + "v"); 
}

 private static void STAClipBoard(string myStringWithHtmlCode)
 {
     ClipClass clipClass = new ClipClass();
     clipClass.myString = myString;
     System.Threading.Thread t = new System.Threading.Thread(clipClass.CopyToClipBoard);
        t.SetApartmentState(System.Threading.ApartmentState.STA);
        t.Start();
        t.Join();
    }


}//class

public class ClipClass
{
    public string myString;

    public void CopyToClipBoard()
    {
        Clipboard.SetText(description);
    }
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top