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

发布于 2025-01-10 07:45:45 字数 631 浏览 0 评论 0原文

我编写了一个简单的登录测试,

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);
        }

    }
}

该程序将单击登录并登录并查找部分视图输入密码,但它只是将用户名中的值插入并单击提交,但从未找到密码。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一曲爱恨情仇 2025-01-17 07:45:45

问题是,在第一页上您有用户名可点击按钮,一旦您单击该按钮,它会将您重定向到一个新页面,您可以在其中提供密码。

但是,这个新页面未正确呈现,这就是您

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

在 C#-Selenium 绑定中,您可以使用 显式等待 的原因,如下所示:

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

现在输入是一个 Web 元素,您可以输入使用 SendKeys 方法获取密码。

input.SendKeys("your 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.

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:

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.

input.SendKeys("your password");

Reference link

萌吟 2025-01-17 07:45:45

通过get()调用URL后,填写EmailOrPhoneNumber字段并单击提交按钮密码字段需要一些时间才能可见并且可交互


解决方案

在这些情况下,要在所需元素上调用 Click(),您必须引入 WebDriverWait 对于 ElementToBeClickable ,您可以使用以下定位器策略

  • Id

    new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.Id("密码"))).Click();
    
  • CssSelector

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

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

参考资料

您可以在以下位置找到一些相关的详细讨论:

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.


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:

  • Id:

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

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("#Password"))).Click();
    
  • 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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文