Selenium执行SendKeys(Key.Enter)时Chrome崩溃

时间:2019-03-07 09:32:12

标签: visual-studio selenium google-chrome automated-tests selenium-chromedriver

我有一个自动化测试,下面有以下代码行,用于在sendkeys之后输入searchbox:

driver.FindElement(By.XPath("//*[@id='standard-uncontrolled']")).SendKeys("@Auto");
driver.FindElement(By.XPath("//*[@id='standard-uncontrolled']")).SendKeys(Keys.Enter);

当我在调试模式下缓慢运行时,它可以完美运行。但是如果没有调试模式,Chrome在执行SendKeys(Keys.Enter)时会崩溃,并且Visual Studio会显示以下消息:

Message: Test method threw exception: 
OpenQA.Selenium.WebDriverException: Unexpected error. 
System.Net.WebException: Unable to connect to the remote server ---> 
System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:64847
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
at System.Net.HttpWebRequest.GetRequestStream()
at OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)

这是我遇到问题的元素的HTML

<th class="jss1543 jss1544 jss1534 jss1527 percent-40" scope="col">
    <div class="jss1536">
        <div class="jss1551 jss1552 jss1528">
            <div class="jss1568 jss1555 jss1530 jss1569 jss1556">
                <input aria-invalid="false" class="jss1578 jss1563 jss1531" id="standard-uncontrolled" placeholder="Search" type="text" value="">
            </div>
        </div>
    </div>
</th>

更新我正在使用的脚本:

using System;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using SeleniumExtras.WaitHelpers;
using OpenQA.Selenium.Interactions;
using System.Threading;

namespace AutoKaercher
{
    [TestClass]

    public class CRUD_Languages
    {
        private TestContext testContextInstance;
        private IWebDriver driver;
        private string intranetURL;
        private string languagesName;
        private string languagesCode;

        [TestMethod]
        [TestCategory("Chrome")]
        public void _CRUD_Languages()
        {
            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));

            //Login
            driver.Navigate().GoToUrl(intranetURL + "/");
            driver.Manage().Window.Maximize();
            driver.FindElement(By.Name("username")).SendKeys("globaladmin");
            driver.FindElement(By.Name("password")).SendKeys("1234@Abcd");
            driver.FindElement(By.XPath("//*[@id='root']/div/div[2]/div/form/div[2]/div[3]/button")).Click();

            //Go to Languages
            wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.XPath("//*[@id='root']/div/div[2]/header/div/div/div[1]/span[2]"))).Click();
            wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.XPath("//*[@id='root']/div/header/nav/div/ul/li[1]"))).Click();

            //Create Languages
            wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.XPath("//*[@id='root']/div/div[3]/div/div[1]/div[1]/div/button"))).Click();
            driver.FindElement(By.XPath("//*[@id='body']/div[2]/div[2]/div[2]/div/div[1]/div/div/input")).SendKeys("@Automation Testing");
            driver.FindElement(By.XPath("//*[@id='body']/div[2]/div[2]/div[2]/div/div[2]/div/div/input")).SendKeys("@AT");
            driver.FindElement(By.XPath("//*[@id='body']/div[2]/div[2]/div[2]/div/div[4]/button[2]")).Click();

            //Check for create Languages successfully
            wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.XPath("//*[@id='root']/div/div[1]/div/div"))).Click();
            wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath("//*[@id='root']/div/div[3]/div/div[1]/div[2]/div/table/tbody/tr[1]/td[1]")));
            driver.FindElement(By.XPath("//*[@id='standard-uncontrolled']")).SendKeys("@Auto");
            driver.FindElement(By.XPath("//*[@id='standard-uncontrolled']")).SendKeys(Keys.Enter);

            //IWebElement searchLanguage = driver.FindElement(By.XPath("//*[@id='standard-uncontrolled']"));
            //searchLanguage.SendKeys("@Auto");
            //Actions inputEnter = new Actions(driver);
            //inputEnter.SendKeys(searchLanguage, Keys.Enter);

            wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath("//*[@id='root']/div/div[3]/div/div[1]/div[2]/div/table/tbody/tr[1]/td[1]")));
            languagesName = driver.FindElement(By.XPath("//*[@id='root']/div/div[3]/div/div[1]/div[2]/div/table/tbody/tr/td[1]")).Text;
            languagesCode = driver.FindElement(By.XPath("//*[@id='root']/div/div[3]/div/div[1]/div[2]/div/table/tbody/tr/td[2]")).Text;
            if (languagesName != "@Automation Testing")
            {
                driver.Quit();
            }
            if (languagesCode != "@AT")
            {
                driver.Quit();
            }

            //Edit Languages
            driver.FindElement(By.XPath("//*[@id='root']/div/div[3]/div/div[1]/div[2]/div/table/tbody/tr/td[3]/div/span[1]")).Click();
            wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath("//*[@id='body']/div[2]/div[2]/div[2]/div/div[1]/div/div/input"))).Clear();
            driver.FindElement(By.XPath("//*[@id='body']/div[2]/div[2]/div[2]/div/div[1]/div/div/input")).SendKeys("@Automation Testing Edited");
            driver.FindElement(By.XPath("//*[@id='body']/div[2]/div[2]/div[2]/div/div[2]/div/div/input")).Clear();
            driver.FindElement(By.XPath("//*[@id='body']/div[2]/div[2]/div[2]/div/div[2]/div/div/input")).SendKeys("@AT-@at");
            driver.FindElement(By.XPath("//*[@id='body']/div[2]/div[2]/div[2]/div/div[6]/button[2]")).Click();

            //Check for edit Languages successfully
            wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.XPath("//*[@id='root']/div/div[1]/div/div"))).Click();
            languagesName = driver.FindElement(By.XPath("//*[@id='root']/div/div[3]/div/div[1]/div[2]/div/table/tbody/tr/td[1]")).Text;
            languagesCode = driver.FindElement(By.XPath("//*[@id='root']/div/div[3]/div/div[1]/div[2]/div/table/tbody/tr/td[2]")).Text;
            if (languagesName != "@Automation Testing Edited")
            {
                driver.Quit();
            }
            if (languagesCode != "@AT-@at")
            {
                driver.Quit();
            }

            //Delete Languages
            driver.FindElement(By.XPath("//*[@id='root']/div/div[3]/div/div[1]/div[2]/div/table/tbody/tr/td[3]/div/span[2]")).Click();
            wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.XPath("//*[@id='intranet-dialog']/div[2]/div/div/div[3]/button[2]"))).Click();

            //Check for delete Languages successfully
            wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.XPath("//*[@id='root']/div/div[1]/div/div"))).Click();
            driver.FindElement(By.XPath("//*[@id='standard-uncontrolled']")).Clear();
            driver.FindElement(By.XPath("//*[@id='standard-uncontrolled']")).SendKeys("@Auto");
            driver.FindElement(By.XPath("//*[@id='standard-uncontrolled']")).SendKeys(Keys.Enter);
            if (driver.FindElements(By.XPath("//*[@id='root']/div/div[3]/div/div[1]/div[2]/div/table/tbody/tr/td[1]")).Count != 0)
            {
                driver.Quit();
            }

        }

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        [TestInitialize()]
        public void SetupTest()
        {
            intranetURL = "https://rkistage.app.kaercher.com";

            //Open Chrome in Incognito mode
            ChromeOptions options = new ChromeOptions();
            options.AddArguments("--incognito");
            driver = new ChromeDriver(options);

            driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(10);
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);

        }

        [TestCleanup()]
        public void MyTestCleanup()
        {
            driver.Quit();
        }
    }
}

我正在使用Chrome版本73.0.3683.86和ChromeDriver版本73.0.3683.68。

有人知道为什么会发生吗?预先感谢!

1 个答案:

答案 0 :(得分:1)

看一些文档,看来72.0.3626.121是铬版本,与Chromedriver 72.0.3626.69不兼容。据我所知,您有两种选择:

  1. 使用Chrome版本72.0.3626.81,不要再使用chromedriver。

  2. 保留浏览器版本并使用chromedriver-72.0.3626.121-202.1.x86_64.rpm

=========

以上内容不是您的问题。继续...

我想我知道那条线怎么了

driver.FindElement(By.XPath("//*[@id='standard-uncontrolled']")).SendKeys(Keys.Enter);

Keys.Enter来自Keys class,首先,您必须导入(也许/可能您已经导入),但更重要的是,它只能由Actions调用。 here's SendKeys认为您正在使用。请注意弃用警告:[Obsolete("Use the Actions or ActionBuilder class to simulate keyboard input.")]

所以无论如何,如果您尝试以下操作,我认为它应该可以工作。

using OpenQA.Selenium.Keys
using OpenQA.Selenium.Interactions;

IWebElement standardUncontrolled = driver.FindElement(By.XPath("//*[@id='standard-uncontrolled']"));
standardUncontrolled.SendKeys("@Auto");


Actions builder = new Actions(driver);        
builder.SendKeys(standardUncontrolled, Keys.Enter).Perform();
Thread.Sleep(1000);