我在selenium webdriver中找到元素时遇到错误 [英] I am getting the error while locating the element in selenium webdriver

查看:83
本文介绍了我在selenium webdriver中找到元素时遇到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上面的代码是我的密码文本字段的html代码,所以,我如何通过xpath找到元素



我尝试了什么:



我正在尝试自动化我的网络应用程序,所以在找到密码元素时我会收到失败:登录

org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与
进行交互
命令持续时间或超时:10.59秒

请帮我解决问题

解决方案

您没有提供任何代码,但我想问的问题与以下相关



*你有两个没有id的密码文本框。这很好,但它很难定位

*你有两个密码文本框,1个用于登录,1个用于注册。您已在评论中指出,您在注册页面上收到错误消息,说明该元素已隐藏。这是因为它的目标是登录下的第一个文本框而不是第二个正在注册的文本框



你没有说明你使用selenium的语言,所以我'我将用C#提供我的例子。



我看到它的方式,你有两个解决方案



1)将唯一ID添加到您拥有的两个密码文本框中。 ex:txt-login-pwd用于登录屏幕,然后txt-registration-pwd用于注册密码模式窗口。



在selenium中你可以通过 driver.FindElement(By.Id(txt-login-pwd))来简单地定位它; driver.FindElement(By.Id(txt-registration-pwd));



2)如果你对这个网站有完全的控制权,我不知道你为什么不把id添加到密码文本框中,但如果你觉得反对,这是你的另一个选择完全比简单地将Id添加到你的两个密码文本框中更多的工作。



要知道你的意思是你必须找到一些能让你的密码文本框独特的东西我唯一看到的是type =password。你需要找出适当的XPATH语法。



注意,我在内存中输入这个,所以它可能编译也可能不编译,但代码将指向正确的方向



  var  pwdTextBoxes = driver.FindElements (By.Xpath(  // input [@ type ='password'])); 
var count = pwdTextBoxes.Count();





此时你会看到 cound 应该说2.这是因为你的登录/注册文本框。



您需要知道pwdTextBoxes列表/数组的哪个索引分别用于注册和登录屏幕。从那里,你将能够定位正确的。



示例: var loginPwdTxtBox = pwdTextBoxes [0]; 将是登录密码文本框,然后 var registrationPwdTxtBox = pwdTextBoxes [1]; 指向注册文本框,您可以对其执行任何操作时间。


the above code is my html code of the Password Text field so, How Can I locate the Element by xpath

What I have tried:

I am trying to automate the my web application, so while locating the element of Password I am getting "FAILED: login
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Command duration or timeout: 10.59 seconds"
Please help me to resolve the Issue

解决方案

You've provided no code but I image the issue is related to the following

* You've got two password textboxes with no id's. This is fine but it makes it extremely hard to target
* You've got two password textboxes, 1 for login, 1 for registration. You've indicated in your comments you get the error on registration page saying the element is hidden. It is because it is targeting the first text box under login instead of the second one under registration

You don't indicate what language you are using selenium with so I'll provide my examples in C#

The way i see it, you've got two solutions

1) Add unique Id's to the two password textboxes that you have. ex: txt-login-pwd for the login screen and then txt-registration-pwd for the registration password modal window.

That way in selenium you can simply target it by doing driver.FindElement(By.Id("txt-login-pwd")); or driver.FindElement(By.Id("txt-registration-pwd"));.

2) If you've got full control over this website I don't know why you wouldnt just add id's to the password textboxes but in the event you feel opposed to that, this is your other option which is entirely more work than simply adding Id's to your two password textboxes.

To see what i mean by the fact that you have to find something that makes your password textboxes unique which the only thing I see is type="password". You'll need to figure out the proper XPATH syntax for this.

Note, i'm typing this from memory so it may or may not compile but the code will point you in the right direction

var pwdTextBoxes = driver.FindElements(By.Xpath("//input[@type='password']"));
var count = pwdTextBoxes.Count();



At this point you'll see cound should say 2. Thats because of your login/registration textboxes.

You'll need to know which index of the pwdTextBoxes list/array is for registration and login screen respectively. From there, you'll be able to targe the correct one.

Example: var loginPwdTxtBox = pwdTextBoxes[0]; would be the login password textbox and then var registrationPwdTxtBox = pwdTextBoxes[1]; points to the registration textbox and you can take whatever action on it that you need at that time.


这篇关于我在selenium webdriver中找到元素时遇到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆