【发布时间】:2013-12-13 14:35:30
【问题描述】:
我是新手,输入用户名/密码后,我可以选择“测试”选项。第一个选项显示在屏幕上,旁边是下拉箭头。单击箭头或框。将打开一个新框,其中包含可供选择的选项(测试 1(默认)、测试 2、测试 3 和测试 4)。我需要根据变量中设置的参数进行选择。我可以通过 xpath 设置选择确切的选项,但无法成功使用变量进行选择。
下拉框是自定义框 - 鼠标悬停时突出显示选择。
提前致谢
以下是 HTML 代码。
<div id="loginForm:selectProject_panel" class="ui-selectonemenu-panel ui-widget-content ui-corner-all ui-helper-hidden ui-shadow" style="width: 208px; display: none; top: 58px; left: 617.5px;">
<div class="ui-selectonemenu-items-wrapper" style="height:auto">
<ul class="ui-selectonemenu-items ui-selectonemenu-list ui-widget-content ui-widget ui-corner-all ui-helper-reset">
<li class="ui-selectonemenu-item ui-selectonemenu-list-item ui-corner-all" data-label="Test 1 ">Test 1</li>
<li class="ui-selectonemenu-item ui-selectonemenu-list-item ui-corner-all" data-label="Test 2 ">Test 2 </li>
<li class="ui-selectonemenu-item ui-selectonemenu-list-item ui-corner-all ui-state-highlight" data-label="Test 3">Test 3</li>
<li class="ui-selectonemenu-item ui-selectonemenu-list-item ui-corner-all" data-label="Test 4">Test 4</li>
</ul>
</div>
</div>
WebDriver 代码
WebElement username = driver.findElement(By.id("loginForm:userLoginId"));
username.sendKeys("username");
WebElement endKeys("Password1!");
WebElement dropdown = driver.findElement(By.xpath(".//*@id='loginForm:selectProject_label']"));
dropdown.click();
// System.out.println();
driver.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
WebDriverWait waitForOptions = new WebDriverWait(driver,3);
waitForOptions.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='loginForm:selectProject']/div[3]")));
driver.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
WebElement option = driver.findElement(By.xpath(".//*[@id='loginForm:selectProject_panel']/div/ul/li[3]"));
option.click();
System.out.println("Selecting Test 3");
WebElement button = driver.findElement(By.id("loginForm:loginButton"));
button.click();
System.out.println(driver.getTitle());
【问题讨论】:
-
问题:它是一个实际的下拉菜单吗?还是有人用 div 和/或 span 制作的自定义下拉菜单?
-
它是一个自定义的下拉菜单,, ty
-
你不需要在每个动作之前设置implicitWait。该时间在设置后对整个会话全局适用。显式等待也比隐式等待更受欢迎。
-
很抱歉,您写的内容一团糟。您缺少密码字段的元素。您正在设置隐式等待多次而不是一次。您正在编写您认为需要的每个元素并在它们之间执行操作。请阅读 Selenium 文档:seleniumhq.org/docs 和 javadocs:selenium.googlecode.com/git/docs/api/java/index.html
-
谢谢。复制时有一些错别字。我遇到的主要问题是如何使用变量检查选项。 Xpath 比较似乎不起作用