Selenium - salesforce.com如何在下拉列表中选择项目

时间:2014-09-09 22:38:48

标签: drop-down-menu selenium-webdriver click salesforce

我需要帮助。我一直在尝试从下拉菜单中自动选择项目。我尝试了很多方法,但我无法让它发挥作用。我使用Selenium 2.39 firefox。示例应用程序位于https://www.salesforce.com/form/signup/freetrial-sales.jsp?d=70130000000EqoP&internal=true

任何帮助将不胜感激。感谢

Xpath是://*[@id='form-container']/ul/li[14]/div/div[2]/a/span[1]

包围:

<div id="form-container" class="clearfix wide-fields style-placeholder">
   <div id="globalErrorMessage" style="display:none"> Please fill out the highlighted fields below </div>   
      <ul class="clearfix vertical form-ul">        
         <li class=" type-hidden">
         <li class=" type-hidden">
         <li class=" type-hidden">
         <li class=" type-hidden">
         <li class=" type-hidden">
         <li class=" type-hidden">
         <li class=" type-hidden">
         <li class=" type-text">
         <li class=" type-text">
         <li class=" type-text">
         <li class=" type-text">
         <li class=" type-text">
         <li class=" type-text">
         <li class=" type-select">
            <div class="control-container error">
            <div class="label">
            <div class="field">
               <select id="CompanyEmployees" class="selectBox" name="CompanyEmployees"  style="display: none;">
                  <a class="selectBox selectBox-dropdown" style="width: 296px; display: inline-block; -moz-user-select: none; "title="" tabindex="0">
                     <span class="selectBox-label" style="width: 262px;">Employees</span>
                     <span class="selectBox-arrow" />
                  </a>
                  <span class="form-field-error show-form-field-error">Select the number of employees</span>
               </div>
               <div class="info">
            </div>
         </li>
         <li class=" type-hidden">
         <li class=" type-hidden">

我已经尝试过使用Select类但不起作用。

     Select select = new Select(driver.findElement(By.name("CompanyEmployees")));
     select.selectByValue("250");

我收到以下错误:

.... 引起:org.openqa.selenium.remote.ErrorHandler $ UnknownServerException:元素当前不可见,因此可能无法与之交互 ...

2 个答案:

答案 0 :(得分:0)

有两点需要注意:

  1. 这不是下拉控件。您可能必须通过一些基本的HTML源代码来了解如何在不使用实际DropDown控件的情况下实现DropDown场景。
  2. 转义序列(注意XPath表达式中的引号)
  3. 下面是C#代码。我相信你可以用你选择的语言找出代码。

    Driver.FindElement(By.XPath(@"(//*[@id=""form-container""])/ul/li[14]/div/div[2]/a/span[1]")).Click(); //Click on the DropDown
    
    Driver.FindElement(By.XPath(@"/html/body/ul[1]/li[4]/a")).Click(); //selects "21 - 100 Employees"
    

答案 1 :(得分:0)

请为&#34;员工&#34;尝试以下代码选择:

public void selectEmployees(){

    WebDriver driver = new FirefoxDriver();
    WebDriverWait driverWait = new WebDriverWait(driver, 30);

    driver.get("https://www.salesforce.com/form/signup/freetrial-sales.jsp?d=70130000000EqoP&internal=true");

    // Wait for visibility of Employees combobox and click on it to open drop-down list
    // Combo-box is selected by css locator which searches for ".selectBox" element that are preceded by "#CompanyEmployees" element
    WebElement emplyeesCombobox = driverWait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#CompanyEmployees ~ .selectBox")));
    emplyeesCombobox.click();

    //Wait for visibility of required drop-down list item ("6 - 20 employees" in this example) and select it by clicking
    // Drop-down list item is selected by XPath locator which searches for "a" element with "6 - 20 employees" text
    WebElement itemToSelect = driverWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[text()='6 - 20 employees']")));
    itemToSelect.click();
}

希望它会有所帮助。