Web driver skips special characters - “(” and “&” characters when using sendkeys

天大地大妈咪最大 提交于 2020-04-16 04:56:28

问题


As part of my automated tests, I need to type a string with special characters ("(PT books)") into a text box on a web application.

When I run the test, "(" is always skipped. There is no issue with ")". I started investigating it and tried all special characters that require a shift key to be pressed ("~!@#$%^&*()_+{}|:<>?") and found that it always skipped "(" and "&". I also tried the same test on the Google search box and in this case all characters were displayed.

I am using Selenium Web driver(2.25.0) + Java + Firefox(15.0.1). I also have setEnableNativeEvents to false in my firefox profile.

I also tried this using Internet Explorer and it works fine. I would like to stick to firefox as most of my tests run on firefox.

Any ideas as to why "(" and "&" are not displayed on the web application I am testing?

Here is the code that I am using

FirefoxProfile profile = new FirefoxProfile();
profile.setEnableNativeEvents( false);
WebDriver driver = new FirefoxDriver(profile);

String searchTerm = "~!@#$%^&*()_+{}|:<>?" ;

String Link = "http://" ;

driver.get( "http://www.google.com");
driver.findElement(By. id("gbqfq")).sendKeys(searchTerm);

driver.get(Link);
driver.findElement(By. id("ctl00_ctl00_FindField_FindField_ctl00_findFieldLinks_basic")).click();
driver.findElement(By. id(UIMapping.SearchBoxId)).sendKeys(searchTerm);

回答1:


I also faced the same issue. I have a simple workaround for this, which is not the kind of solution i am looking for but time being this helps. If there is a better way to handle please suggest.

        driver = new FirefoxDriver();
        String searchKey = "TShirt(M)";
        driver.Navigate().GoToUrl("http://mywebsite.com/");

        if (searchKey.Contains("(") || searchKey.Contains("&"))
        {
            ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].value ='" + searchKey + "';", driver.FindElement(By.Id("search_text"))); 
        }
        else
        {
            driver.FindElement(By.Id("search_text")).SendKeys(searchKey);
        }

        driver.FindElement(By.Id("search_button")).Click();


来源:https://stackoverflow.com/questions/12603210/web-driver-skips-special-characters-and-characters-when-using-sendkeys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!