如何从html的下拉菜单中读取元素?

时间:2015-05-25 12:24:36

标签: java scala selenium-webdriver

我正在通过Selenium FirefoxDriver解析以下html -

      <div id="primaryNav" style='background: url("https://images-na.ssl-images-amazon.com/images/G/31/associates/network/08-ui-elements/primaryNavBackground._V161555288_.gif") repeat-x bottom;'> 
       <div id="menuh">
        <ul>
            <li style="visibility:hidden; 
        height:24px"/>


               <li style='background:url("https://images-na.ssl-images-amazon.com/images/G/31/associates/network/08-ui-elements/tab-unslected-right._V161557413_.gif") no-repeat right top;'>
                    <a href="/gp/associates/promo/buildlinks.html" style='float:left;background:url("https://images-na.ssl-images-amazon.com/images/G/31/associates/network/08-ui-elements/tab-unslected-left._V161554471_.gif") no-repeat left top;'>Links & Banners<span class="droparrow">&#x25BC;</span></a>

    <div class="parent">
        <div class="dropdownlinks">
            <div class="subitem"><a href="/gp/associates/network/build-links/individual/main.html">Product Links</a></div><div class="subitem"><a href="/gp/associates/network/build-links/banner/main.html">Banner Links</a></div><div class="subitem"><a href="/gp/associates/network/build-links/text/main.html">Link to Any Page</a></div><div class="subitem"><a href="/gp/associates/network/tools/link-checker/main.html">Link Checker</a></div>
        </div>
    </div>

                </li>


                <li style='background:url("https://images-na.ssl-images-amazon.com/images/G/31/associates/network/08-ui-elements/tab-unslected-right._V161557413_.gif") no-repeat right top;'>
                    <a href="http://widgets.amazon.in/?_encoding=UTF8&amp;store=httpswwwvanta-21&amp;tag=httpswwwvanta-21" style='float:left;background:url("https://images-na.ssl-images-amazon.com/images/G/31/associates/network/08-ui-elements/tab-unslected-left._V161554471_.gif") no-repeat left top;'>Widgets</a>
                </li>


                <li style='background:url("https://images-na.ssl-images-amazon.com/images/G/31/associates/network/08-ui-elements/tab-unslected-right._V161557413_.gif") no-repeat right top;'>
                    <a href="/gp/advertising/api/detail/main.html" style='float:left;background:url("https://images-na.ssl-images-amazon.com/images/G/31/associates/network/08-ui-elements/tab-unslected-left._V161554471_.gif") no-repeat left top;'>Product Advertising API</a>
                </li>


                <li style='background:url("https://images-na.ssl-images-amazon.com/images/G/31/associates/network/08-ui-elements/tab-unslected-right._V161557413_.gif") no-repeat right top;'>
                    <a href="/gp/associates/network/reports/main.html" style='float:left;background:url("https://images-na.ssl-images-amazon.com/images/G/31/associates/network/08-ui-elements/tab-unslected-left._V161554471_.gif") no-repeat left top;'>Reports<span class="droparrow">&#x25BC;</span></a>


    <div class="parent"><div class="dropdownlinks">

    <div class="subitem"><a href="/gp/associates/network/reports/report.html?ie=UTF8&amp;reportType=earningsReport" >Earnings Report</a>
</div><div class="subitem"><a href="/gp/associates/network/reports/report.html?ie=UTF8&amp;reportType=ordersReport" >Orders Report</a>
</div><div class="subitem"><a href="/gp/associates/network/reports/report.html?ie=UTF8&amp;reportType=linkTypeReport" >Link-Type Report</a>
</div><div class="subitem"><a href="/gp/associates/network/reports/report.html?ie=UTF8&amp;reportType=trendsReport" >Daily Trends</a>
</div><div class="subitem"><a href="/gp/associates/network/reports/report.html?ie=UTF8&amp;reportType=tagsReport" >Tracking ID Summary Report</a>
</div>
</div>
</div>
                </li>
        </ul>
       </div>
      </div>

我正在尝试从下拉菜单中选择“收入报告”。 我试过这个 -

dropDownButton: WebElement = driver.findElement(By.xpath(".//a[@href='https://affiliate-program.amazon.in/gp/associates/network/reports/report.html?ie=UTF8&reportType=earningsReport']"))
dropDownButton.click()

我也尝试过这样 -

val dropDownButton = driver.findElement(By.linkText("Earnings Report"))
dropDownButton.click()

在这两种情况下,代码仅在我将鼠标悬停在下拉菜单上时运行。无需手动点击。

我还尝试了以下代码,我不确定是否正确 -

     import scala.collection.JavaConversions._
      def selectValueFromDropdown( value: String) = {
      var options = driver.findElements(By.id("menuh"));
      for(option <- options) {
            if (value.equals(option.getText())) {
                option.click()
            }
        }

      }

selectValueFromDropdown("Earnings Report")

我有点迷失在这里。请使用Java或Scala建议解决方案。

编辑:我从主页面登录后进入此页面。这可能是个问题吗?

4 个答案:

答案 0 :(得分:3)

请尝试:

  • 首先选择下拉列表,然后按值或索引选择 -

Select drpdown = new Select(driver.findElement(By.xpath("Locator of the dropdown")); drpdown.SelectByValue("Earning Report");

  • 如果“收入报告”是可见文本,那么 -

drpdown.selectByVisibleText("Earning Report");

答案 1 :(得分:1)

正如您所提到的,您必须将鼠标悬停在下拉菜单上才能正常工作。您的菜单还有子菜单。因此,在点击链接之前,您需要使用&#34;执行&#34; &#34;动作&#34;的方法。通过这种方式,它允许Selenium在按住菜单的同时发现特定的子菜单。代码是:

val menuElement = driver.findElement(By.id("menuh"))
/* If the css selector used below does not match the element that
*  fires the hover action then check which element fires it and update
*  the selector */
val subMenuElement = driver.findElement(By.cssSelector("#menuh li:nth-child(5)"))
val earningsReportElement = driver.findElement(By.linkText("Earnings Report"))
val action = new Actions(driver)

action.moveToElement(menuElement).perform()
action.moveToElement(subMenuElement).perform()
action.moveToElement(earningsReportElement)
action.click()
action.perform()

答案 2 :(得分:0)

试试这个 -

 driver.findElement(
            By.xpath("//a[contains(@href,'earningsReport')]"))
            .click();

答案 3 :(得分:0)

@Dagojvg给出的解决方案在重新安排表达式之后起作用。

import org.openqa.selenium.interactions.Actions
val action = new Actions(driver)
val menuElement = driver.findElement(By.id("menuh"))
action.moveToElement(menuElement).perform()

val subMenuElement = driver.findElement(By.cssSelector("#menuh li:nth-child(5)"))
action.moveToElement(subMenuElement).perform()

val earningsReportElement = driver.findElement(By.linkText("Earnings Report"))
action.moveToElement(earningsReportElement)

action.click()
action.perform()