简体   繁体   English

没有这样的元素:无法定位元素:{"method":"css selector","selector":"#Password"} 使用 Selenium 和 C#

[英]no such element: Unable to locate element : {"method":"css selector","selector":"#Password"} using Selenium and C#

I wrote a simple test for login我写了一个简单的登录测试

namespace Seleniumtest.Customer
{
    [TestFixture]
    class LoginTest :TestBase
    {
        [Test]
        public void LoginWithPhoneNumber()
        {
            webDriver.Navigate().GoToUrl("https://test123.ifc.ir/login");
            webDriver.FindElement(By.Id("EmailOrPhoneNumber")).SendKeys("09108599423");
            webDriver.FindElement(By.ClassName("buttons")).Click();
            var input = webDriver.FindElement(By.Id("Password"));     
            Assert.That(input.Displayed, Is.True);
        }

    }
}

This program will click login and have login and find partial view input password but its just instert the value in username and click the submit but never find password.该程序将单击登录并登录并找到部分视图输入密码,但它只是在用户名中插入值并单击提交但永远找不到密码。

The issue is that on page one you have username and clickable button, and once you click on the button it will redirect you to a new page where you can provide the password.问题是在第一页上你有用usernameclickable按钮,一旦你点击按钮,它会将你重定向到一个新页面,你可以在其中提供密码。

However, this new page is not rendered properly and that's the reason you are getting但是,这个新页面没有正确呈现,这就是你得到的原因

no such element: Unable to locate element : {"method":"css selector","selector":"#Password"} 

In C#-Selenium bindings, you can use the explicit wait like below:在 C#-Selenium 绑定中,您可以使用如下所示的explicit wait

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement input = wait.Until(e => e.FindElement(By.Id("Password")));

and now the input is a web element, you can input the password with SendKeys method.现在输入的是一个web元素,可以用SendKeys方法输入密码。

input.SendKeys("your password");

Reference link参考链接

Once you invoke the url through get() , fillup the EmailOrPhoneNumber field and click on the Submit button it would need a little while for the Password field to be visible and interactable .通过get()调用 url 后,填写EmailOrPhoneNumber字段并单击提交按钮,密码字段需要一段时间才能显示交互


Solution解决方案

In these cases to invoke Click() on the desired element you have to induce WebDriverWait for the ElementToBeClickable and you can use either of the following locator strategies :在这些情况下,要在所需元素上调用Click() ,您必须为ElementToBeClickable引入WebDriverWait ,并且您可以使用以下任一定位器策略

  • Id :编号

     new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.Id("Password"))).Click();
  • CssSelector : CSS选择器

     new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("#Password"))).Click();
  • XPath : XPath :

     new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@id='Password']"))).Click();

References参考

You can find a couple of relevant detailed discussions in:您可以在以下位置找到一些相关的详细讨论:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM