C# 元素可选择';不工作';

C# 元素可选择';不工作';,c#,selenium,selenium-webdriver,C#,Selenium,Selenium Webdriver,上述代码按原样工作。但是,如果我取消对wait.Until的注释,则浏览器将打开,但不会发送任何文本。它似乎在等待处“停止”。不知道我做错了什么。对于当前用例来说,这不是一个问题,但是如果知道如何为将来的使用正确地执行它,那就太好了 编辑: 在做了一些搜索和测试之后,我发现下面的代码特别适用于RemoteWebDriver IWebDriver driver = new RemoteWebDriver(uri, dc); WebDriverWait wait = new WebDriverWai

上述代码按原样工作。但是,如果我取消对wait.Until的注释,则浏览器将打开,但不会发送任何文本。它似乎在等待处“停止”。不知道我做错了什么。对于当前用例来说,这不是一个问题,但是如果知道如何为将来的使用正确地执行它,那就太好了

编辑: 在做了一些搜索和测试之后,我发现下面的代码特别适用于RemoteWebDriver

IWebDriver driver = new RemoteWebDriver(uri, dc);
WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0,0,60));
driver.Navigate().GoToUrl("https://google.com/ncr");
string xpath = "//*[@id=\"lst-ib\"]";  // the google search text box
IWebElement element = driver.FindElement(By.XPath(xpath));
//wait.Until(ExpectedConditions.ElementToBeClickable(element));
element.Click();
element.SendKeys("some text");
当“name”存在于DOM中时,它会工作,当我将“name”修改为DOM中不存在的内容时,它会抛出异常,这是意料之中的。
谢谢@DebanjanB把我送到正确的方向

你能试试下面的代码吗。工作正常

IWebDriver driver = new RemoteWebDriver(uri, dc);
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(60);
IWebElement element = driver.FindElement(By.Name(name));

如果您面临任何挑战,请务必告诉我,如果您查看
网页上的
搜索框
表示的
,该元素如下所示:

public static void search() throws Throwable
    {
        WebElement SearchBox = driver.findElement(By.xpath(".//*[@id='ctl00_PlaceHolderSearchArea_SmallSearchInputBox1_csr_sbox']"));
        SearchBox.clear();
        SearchBox.sendKeys("Text");
        driver.manage().timeouts().implicitlyWait(8000, TimeUnit.SECONDS);
        WebElement SearchLink=driver.findElement(By.xpath(".//*[@id='ctl00_PlaceHolderSearchArea_SmallSearchInputBox1_csr_SearchLink']"));
        SearchLink.click();
        driver.manage().timeouts().implicitlyWait(10000, TimeUnit.SECONDS);
        driver.navigate().back();   
    } 

谢谢你的精彩解释。我将行更改为wait.Until(ExpectedConditions.ElementIsVisible(By.XPath(XPath));但是我得到了同样的结果。不,不幸的是,我得到了同样的结果。有趣的是,当我使用FirefoxDriver而不是RemoteWebDriver时,您的代码可以工作。我一直在谷歌上疯狂地搜索,但似乎找不到原因。这并不能回答问题。是否可能因为元素是文本框而无法检查元素是否可单击(或者更准确地说,您可以尝试等待它,但文本框永远不可单击,代码将超时)?您有code
元素。单击()
,但它可能什么都不做。这不会产生任何问题,除非你告诉selenium等待文本框被点击,但这不会发生。比如说,你为什么不等它出现呢?
<input class="gsfi" id="lst-ib" maxlength="2048" name="q" autocomplete="off" title="Search" type="text" value="" aria-label="Search" aria-haspopup="false" role="combobox" aria-autocomplete="list" dir="ltr" spellcheck="false" style="border: none; padding: 0px; margin: 0px; height: auto; width: 100%; background: url(&quot;data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D&quot;) transparent; position: absolute; z-index: 6; left: 0px; outline: none;">
WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0,0,10));
IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By.Name("q")));
element.SendKeys("some text");