java 如何使用没有 id/name 或任何其他唯一标识符的 selenium 定位元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13379315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to locate an element using selenium that does not have id/name or any other unique identifier?
提问by Mike
There are two fields in the page: Username and Password(Highlighted in the below code with **). Need to enter data into it using selenium. However, both the objects have same identifier elements except tabIndex. Please help me as to How to identify the elements(Please refer html).
页面中有两个字段:用户名和密码(在下面的代码中用**突出显示)。需要使用硒将数据输入其中。但是,这两个对象具有相同的标识符元素,但 tabIndex 除外。请帮助我如何识别元素(请参阅html)。
Please note: The below code worked for me but user will not be able to see the data that is input in the GUI. I want the data to be seen on the UI.
请注意:以下代码对我有用,但用户将无法看到在 GUI 中输入的数据。我希望在 UI 上看到数据。
Thanks, Mike
谢谢,迈克
@FindBy(name="cams_cb_username")
private WebElement emailId;
@FindBy(name="cams_cb_password")
private WebElement password;
((JavascriptExecutor)DriverFactory.getDriver()).executeScript("arguments[0].setAttribute('value',arguments[1]);", emailId, "username");
((JavascriptExecutor)DriverFactory.getDriver()).executeScript("arguments[0].setAttribute('value',arguments[1]);", password, "pwd");
Here is the HTML.
这是 HTML。
<form action="/vito-mma/activateLogin.do" method="post" name="loginForm">
<input type="hidden" value="vitocom" name="cams_security_domain">
<input type="hidden" value="/vito-mma/showPlans.do" name="cams_original_url">
<input type="hidden" value="http" name="cams_login_config">
<input type="hidden" value="" name="cams_cb_partner">
<fieldset class="mma-signin">
<div class="clearfix">
<**input class="bigtext e-hint" type="text" value="" name="e_hint_dummy_input" size="25" autocomplete="off" tabindex="1" style="display: inline;"**>
<input class="bigtext e-hint" type="text" title="Email address" value="" tabindex="1" size="25" name="cams_cb_username" style="display: none; background-color: rgb(255, 255, 255);" autocomplete="off">
</div>
<div class="clearfix">
<**input class="bigtext e-hint" type="text" value="" name="e_hint_dummy_input" size="25" autocomplete="off" tabindex="2"**>
<input class="bigtext e-hint" type="password" title="Password" value="" tabindex="2" size="25" name="cams_cb_password" style="display: none; background-color: rgb(255, 255, 255);" autocomplete="off">
</div>
</fieldset>
<div class="indent">
</form>
Note:Tried with Xpath: Did not work. Recorded with IDE for username, (name="cams_cb_username"). Used this - Did not work either.
注意:用 Xpath 试过:没用。使用 IDE 记录用户名,(name="cams_cb_username")。用过这个 - 也没有用。
回答by Pavel Janicek
For those types of fields, its good to use Xpath. Because I am also Xpath newbie, there is way, how I do this:
对于这些类型的字段,最好使用 Xpath。因为我也是 Xpath 新手,所以我有办法做到这一点:
- Install Selenium IDEand click (and type) the field you want to type in
- In the selenium IDE you will see the locator which the IDE used. You can change it further
- 安装Selenium IDE并单击(并输入)您要输入的字段
- 在 selenium IDE 中,您将看到 IDE 使用的定位器。你可以进一步改变它
The xpath will look like //input/div
or something like that. I dont know exactly, here I am really newbie
xpath 看起来像//input/div
或类似的东西。我不知道确切,在这里我真的是新手
In code you then use
在代码中,您然后使用
@FindBy(xpath="/the/xpath/provided/by/selenium_ide")
EDIT
编辑
See this image
看到这张图片
Here I created simple test task with Selenium IDE - open your question, click to search box and type "search"
在这里,我使用 Selenium IDE 创建了简单的测试任务 - 打开您的问题,单击搜索框并输入“搜索”
In the last command, the IDE proposed me the most obvious solution for locator. But I can change that.
在最后一个命令中,IDE 为我提出了最明显的定位器解决方案。但我可以改变这一点。
回答by AxxA Osiris
You could customize the xpath to use the tabindex
attribute and the name
attribute :
您可以自定义 xpath 以使用tabindex
属性和name
属性:
@FindBy(xpath="\input[@name='cams_cb_username' and @tabindex='1']")
private WebElement emailId;
@FindBy(xpath="\input[@name='cams_cb_password' and @tabindex='2']")
private WebElement password;
Or alternatively, you could use something like this :
或者,你可以使用这样的东西:
WebElement username = wait.until(presenceOfElementLocated(By.xpath("\input[@name='cams_cb_username' and @tabindex='1']")));
WebElement password = driver.findElement(By.xpath("\input[@name='cams_cb_password' and @tabindex='2']"));
username.clear();
username.sendKeys(username);
password.clear();
password.sendKeys(password);
password.submit();
Hope this helps.
希望这可以帮助。
回答by Ioan
First of all, on your post you marked different two inputs than the ones you have in the description (@FindBy(name="cams_cb_username")
& @FindBy(name="cams_cb_password")
.
首先,在您的帖子中,您标记的两个输入与描述中的输入不同(@FindBy(name="cams_cb_username")
& @FindBy(name="cams_cb_password")
.
However, for what you've marked with **
, the corresponding CSS selectors are :
但是,对于您用 标记的内容**
,相应的 CSS 选择器是:
First one :
.clearfix:nth-child(1) input[name="e_hint_dummy_input"]
Second one :
.clearfix:nth-child(2) input[name="e_hint_dummy_input"]
第一 :
.clearfix:nth-child(1) input[name="e_hint_dummy_input"]
第二个 :
.clearfix:nth-child(2) input[name="e_hint_dummy_input"]
回答by Virendra Joshi
You can directly use names for locating elements : like 1.For usrname use : selenium.type("cams_cb_username","your UN"); 2.For password use: selenium.type("cams_cb_password","your PWD"); Or u can use like: For username : //input[@name='cams_cb_username']; For PWD : //input[@name='cams_cb_password'];
您可以直接使用名称来定位元素:例如 1.For usrname 使用: selenium.type("cams_cb_username","your UN"); 2.密码使用:selenium.type("cams_cb_password","your PWD"); 或者你可以使用:对于用户名://input[@name='cams_cb_username']; 对于密码://input[@name='cams_cb_password'];
These should work.
这些应该有效。