【问题标题】:How To Handle React Select Component with Selenium IDE?如何使用 Selenium IDE 处理 React Select 组件
【发布时间】:2017-08-27 19:46:11
【问题描述】:

我对 Selenium IDE 位置元素有疑问

这是链接:https://jedwatson.github.io/react-select/

这个命令我成功了:

Command: sendKeys
Target: css=div.Select-control input
Value: Victoria${KEY_ENTER}

但我不知道如何处理具有相同元素的下一个字段

<div class="Select-placeholder">Select...</div>

问题是如何使用 Selenium IDE 处理这个问题?

【问题讨论】:

    标签: selenium automated-tests selenium-ide qa react-select


    【解决方案1】:

    查看您的页面,您不能像第一个字段那样使用css=div.Select-placeholder input 的原因是输入不是占位符元素的子元素。您需要使用像 //div[@class='Select-placeholder']/..//input 这样的一些 xpath,它会找到 Select-placeholder 元素,向上导航到父元素,然后找到输入。但是存在的问题是,有多个输入字段与此匹配。

    您可以使用:(//div[@class='Select-placeholder'])[1]/..//input 将“1”替换为您要定位的元素的任何实例,但如果可以的话,我建议您修改页面本身的代码以使每个输入字段唯一可识别(添加 id 标签例如)因为如果需要更改/添加这些输入的顺序等,它会使您的自动化变得不那么脆弱并且可能会失败。

    【讨论】:

      【解决方案2】:

      你可以这样做

      <span id='your_id'>
      	<Select
      	 className='no_matter'
           options={this.selectItems}
          />
      </span>

      并以这种方式通过 selenium ide 查找

      <tr>
      	    <td>waitForElementPresent</td>
      	    <td>css=[id='your_id']> div > div > div > input</td>
      	    <td></td>
          </tr>

      并在 Select by 中选择项目:

      <tr>
      	    <td>sendKeys</td>
      	    <td>css=[id='your_id]> div > div > div > input</td>
      	    <td>1${KEY_ENTER}</td>
      </tr>

      【讨论】:

        【解决方案3】:

        我可以做到以下几点:

        Actions act = new Actions(driver);//driver variable is chrome web driver ref
        
        WebElement selectInput=driver.findElement(By.className("Select-input"));//Thread.sleep(5000);
        
        act.click(selectInput).build().perform();//Thread.sleep(5000);
        
        //list of all option
        List<WebElement> selectValues=driver.findElements(By.className("Select-option"));//Thread.sleep(5000);
        
        //first option:
        WebElement firstWebElement=selectValues.get(0);//Thread.sleep(5000);
        
        act.click(firstWebElement).build().perform();//Thread.sleep(5000);
        

        我已经评论了线程的睡眠,因为我在本地运行,有时从远程机器上的 UI 获取元素需要时间,所以在这种情况下取​​消注释 Thread.sleep 并尝试。

        【讨论】:

          【解决方案4】:

          使用 Python Selenium,我在创建新选项时遇到了同样的问题:

          menu = selenium.find_element_by_css_selector(".Select")
          (ActionChains(selenium)
              .move_to_element(menu)
              .click()
              .send_keys('whatever new option,'))
              .perform()
          )
          

          尝试将光标移至第一个选项。

          【讨论】:

          • 按值可以,但是如何按索引选择?
          • 如果是新选项,你没有索引
          猜你喜欢
          • 2014-01-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-05-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多