gpt4 book ai didi

java - 如何使用没有 id/name 或任何其他唯一标识符的 selenium 定位元素?

转载 作者:行者123 更新时间:2023-11-29 08:02:48 25 4
gpt4 key购买 nike

页面中有两个字段:用户名和密码(在下面的代码中用**突出显示)。需要使用 Selenium 将数据输入其中。但是,除了 tabIndex 之外,这两个对象都具有相同的标识符元素。请帮助我如何识别元素(请引用 html)。

请注意:以下代码适用于我,但用户将无法看到在 GUI 中输入的数据。我希望在 UI 上看到数据。

谢谢,迈克

@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");

这是 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>

注意:尝试使用 Xpath:没有用。使用 IDE 记录用户名 (name="cams_cb_username")。用过这个 - 也没有用。

最佳答案

您可以自定义 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;

或者,您可以使用这样的东西:

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

希望这对您有所帮助。

关于java - 如何使用没有 id/name 或任何其他唯一标识符的 selenium 定位元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13379315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com