首页 文章

如何使用webdriver从下拉菜单中选择webelement

提问于
浏览
0

我试图从下拉列表中选择一个webelement但不能这样做 . 我试图从下拉列表中选择的webelement是 Edit/ViewResume


1 st试试

在我的第一次尝试中,我试图通过使用Action和Select类方法来选择webelement . 这里从下拉列表中选择元素我尝试了selectByValue(value),selectByIndex(1)和selectByVisibleText(text)但它们都没有工作,我得到 org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "a" 这个例外 .

Webelement wb = driver.findElement(By.xpath("//div[@class='ns_menu_item_wrap ns_lt active']/a"));
  Actions mouse = new Actions(driver);
  mouse.moveToElement(wb).perform();
  Select sel = new Select(wb);
  sel.selectByIndex(1);

2 nd试试

在这里我试图通过使用Action类build(),click()和perform()方法来选择元素,但没有发生任何事情,即,我仍然无法从下拉列表中选择webelement . 对于此代码,我没有任何异常 .

Webelement wb = driver.findElement(By.xpath("//div[@class='ns_menu_item_wrap ns_lt active']/a"));
  Actions mouse = new Actions(driver);
  mouse.moveToElement(wb).click();
  driver.manage().timeouts().implicitlyWait(10 , TimeUnit.SECONDS);
  wb=driver.findElement(By.xpath("//a[contains(@href,'http://my.monsterindia.com/view_resume.html?mode=edit')]"));
  mouse.moveToElement(wb).click();
  driver.manage().timeouts().implicitlyWait(10 , TimeUnit.SECONDS);
  mouse.build();
  Thread.sleep(4000);
  mouse.perform();

HTML片段下拉选项

<div class="ns_menu_item_wrap ns_lt active">
<a class="ns_menu_item" href="http://my.monsterindia.com/my_monster.html">My Monster</a>
<div class="ns_dropdown" style="display: none; left: -2px;">
<div class="ns_dropdown_inner">
<div class="ns_dd_link_wrap">
<a class="ns_dd_link" href="http://my.monsterindia.com/view_resume.html?mode=edit">Edit/View Resume</a>
<a class="ns_dd_link" href="http://my.monsterindia.com/applications.html">My Applications</a>
<a class="ns_dd_link" href="http://my.monsterindia.com/manageagents.html">Job Agent</a>
<a class="ns_dd_link" href="http://my.monsterindia.com/confidentiality.html">Privacy Plus</a>
<a class="ns_dd_link" href="http://my.monsterindia.com/newsletter.html">Subscriptions</a>
</div>
<div class="ns_dd_othertxt">
<div class="ns_clr"/>
</div>
</div>
</div>

3 回答

  • 0

    您提供的标记中没有HTML select 项 . 有关详细信息,请查看here . 你只有那里的链接 . 那你为什么不点击那个链接呢?这将是这样的:

    driver.findElement(By.linkText("Job Agent")).click();
    

    或者您需要点击的任何一个 .

  • 0

    你的第二次尝试看起来有点奇怪,因为在你的动作中你调用 mouse.moveToElement(wb) 然后在执行该动作之前将另一个元素分配给 wb . 我想尝试以下方法:

    driver.manage().timeouts().implicitlyWait(10 , TimeUnit.SECONDS); // It is enough to call this once
    Webelement wb = driver.findElement(By.xpath("//div[@class='ns_menu_item_wrap ns_lt active']/a"));
    Actions mouse = new Actions(driver);
    mouse.moveToElement(wb).click();
    Webelement wb2 = driver.findElement(By.xpath("//a[contains(@href,'http://my.monsterindia.com/view_resume.html?mode=edit')]"));
    mouse.moveToElement(wb2).click();
    mouse.build(); // You don't need the sleep after this before calling perform()
    mouse.perform();
    

    但就像ErkiM一样 . 建议,我不明白为什么简单

    driver.findElement(By.xpath("//a[contains(@href,'http://my.monsterindia.com/view_resume.html?mode=edit')]"));
    

    无法工作 .

  • 0

    因为我无法在评论中粘贴代码,这就是为什么把它作为答案 - 在这段代码中我得到System.out.println(“2”)的输出;为2但没有获得System.out.println(“3”)的任何输出;我在逻辑上做错了吗?

    driver.manage().timeouts().implicitlyWait(10 , TimeUnit.SECONDS);
      wb = driver.findElement(By.linkText("My Monster"));
      Actions mouse = new Actions(driver);
      mouse.moveToElement(wb).click();
      System.out.println("2");
      WebElement wb1 = driver.findElement(By.linkText("Edit/View Resume"));
      mouse.moveToElement(wb1).click();
      mouse.build();
      mouse.perform();
      System.out.println("3");
    

相关问题