【问题标题】:Search from the menu using Selenium webdriver使用 Selenium webdriver 从菜单中搜索
【发布时间】: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 比较似乎不起作用

标签: java selenium webdriver


【解决方案1】:
String optionToSelect = "Test2"

driver.findElement(By.xpath("//*[contains(@class,'ui-selectonemenu-items ui-selectonemenu') and text()='" + optionToSelect  + "') ]";

【讨论】:

  • 线程“main”中的异常 org.openqa.selenium.InvalidSelectorException: 给定的选择器 .//*[ui-selectonemenu-items ui-selectonemenu-list') 和 text()='Test 3 ') ] 无效或不会生成 WebElement。发生以下错误: InvalidSelectorError: Unable to locate an element with the xpath expression .//*[ui-selectonemenu-items ui-selectonemenu-list') and text()='Test 3') ] 因为以下错误: - 我仍然收到错误..提前谢谢
  • 你在我提到的 xpath 中缺少@class。
  • 字符串选项 = "测试 3"; driver.findElement(By.xpath(".//*[contains@class,'ui-selectonemenu-items ui-selectonemenu-list') and text()='" + option + "') ]"));线程“main” org.openqa.selenium.InvalidSelectorException 中的异常:给定的选择器 .//*[contains@class,'ui-selectonemenu-items ui-selectonemenu-list') 和 text()='Test 3')]无效或不生成 WebElement。出现以下错误:InvalidSelectorError: Unable to locate an element with the xpath expression .//*[contains@class,'ui-selectonemenu-items ui-selectonemenu-list') and text()='Test 3')]
  • 我得到同样的错误 - driver.findElement(By.xpath("//*[contains(@class,'ui-selectonemenu-items ui-selectonemenu') 和 text()=' " + optionToSelect + "') ]"; .会不会和jquery ui有关
  • 尝试删除 . before // 并确保元素在单击之前可见。
猜你喜欢
  • 2023-03-20
  • 1970-01-01
  • 2014-08-21
  • 1970-01-01
  • 2012-09-05
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多