【问题标题】:Select Drop Down - Selenium Webdriver选择下拉菜单 - Selenium Webdriver
【发布时间】:2016-04-08 12:23:55
【问题描述】:

我正在演示站点上创建练习测试,但是,我在从下拉列表中选择一个值时遇到问题,我无法找到元素,但是它是正确的 ID,我已经尝试过通过 ID 和 CSS 选择器以及没有运气:( 我将在下面发布 HTML 和 Selenium 代码:​​

HTML

<select id="dropdown_7" name="dropdown_7" class="  piereg_validate[required]"><option value="Afghanistan">Afghanistan</option>

Ruby 代码:

drop_list = @@wait.until {
          drop = @@driver.find_element :id => '#dropdown_7'
          drop if drop.displayed?
          drop.click
        }


        options=drop_list.find_element :id => '#dropdown_7'

        options.each do |i|
          if i.text == 'American Samoa'
            i.click
            break
          end

【问题讨论】:

    标签: ruby selenium selenium-webdriver


    【解决方案1】:

    问题是您将 id 指定为“#dropdown_7”。虽然这是一个匹配 id 为“dropdown_7”的元素的 CSS 选择器,但它不会匹配 id 属性。

    应该是:

    drop = @@driver.find_element :id => 'dropdown_7'
    

    【讨论】:

      【解决方案2】:

      在下面的java中是选择下拉菜单的方式

        Select dropdown = new Select(driver.findElement(By.id("your id")));
      
        dropdown.selectByVisibleText("option text here");
      

         dropdown.selectByIndex(1);
      

         dropdown.selectByValue("value attribute of option");
      

      因此无需在 webdriver 中使用 Select 来点击选项。

      谢谢你, 壁画

      【讨论】:

        【解决方案3】:

        我在 ruby​​ 中的实现方式是:

        Selenium::WebDriver::Support::Select.new(
            @driver.find_element(:how, :what)
        ).select_by(:how, :what)
        

        【讨论】:

          【解决方案4】:

          使用 JavaScript 执行器点击列表中的选项。

          public void javascriptclick(String element)
          { 
              WebElement webElement=driver.findElement(By.xpath(element));
              JavascriptExecutor js = (JavascriptExecutor) driver;
          
              js.executeScript("arguments[0].click();",webElement);   
              System.out.println("javascriptclick"+" "+ element);
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2023-04-02
            • 1970-01-01
            • 1970-01-01
            • 2019-02-25
            • 2017-10-29
            • 2023-03-25
            • 2022-11-11
            相关资源
            最近更新 更多