2017-01-10 169 views
0

我试图自动化一个用户搜索项目&一旦搜索结果出现的情况下,他点击了该特定项目的数量的服务大小&。无法在selenium webdriver中找到元素div元素

enter image description here

但是,我经常收到当我试图通过选择的数量大小,甚至还试图与所有的定位器播放后更改值&操纵文本字段元素未发现异常为这个元素(文本框,下拉几个按钮)发生的div标签。我证实这些元素不属于框架。我也尝试注入JavaScript执行操作,但无济于事。 有人可以指出我哪里出错了。

<div id="loaded_item"> 
<input id="food_entry_food_id" type="hidden" name="food_entry[food_id]" value="348096022"> 
<input id="food_entry_date" type="hidden" value="2017-01-10" name="food_entry[date]"> 
<p class="food-description">Bananas, raw</p> 
<div class="verified-food-container" style="display: block;"> 
<div class="user_submitted"></div> 
<p></p> 
<h3 class="secondary-title">How much?</h3> 
<input id="food_entry_quantity" class="text short" type="text" size="30" name="food_entry[quantity]" autocomplete="off" value="1.0"> 
servings of 
<select id="food_entry_weight_id" class="select" name="food_entry[weight_id]"> 
<p></p> 
<p></p> 
<h3 class="secondary-title">To which meal?</h3> 
<select id="food_entry_meal_id" class="select" name="food_entry[meal_id]"> 
<p></p> 
<input id="update_servings" class="button log" type="submit" value="Add Food To Diary" data-food-id="348096022" data-external-id="63109870792493" data-version-id="197946116099837"> 
<div class="nutritional_info"> 
</div> 

以下是我的代码来查找这些元素。

  @FindBy(xpath=".//*[@id='searchFoodByName']/form/p/input[4]") 
      WebElement searchFoodDatabaseButton; 

      @FindBy(linkText = "Bananas, raw") 
      WebElement matchedFoodThirdOption; 

       @FindBy(xpath="//div[@id='loaded_item']/input[@id='update_servings']") 
      WebElement addFoodToDiary; 

      @FindBy(xpath=".//*[@id='date_controls']/form/span/a[2]") 
      WebElement clickOnNextDate; 

      @FindBy(xpath=".//*[@id='food_entry_quantity']") 
      WebElement foodEntryQuantity; 

      @FindBy(css = "select[id='food_entry_weight_id']") 
      WebElement foodEntryWeight; 

      @FindBy(css = "select[id='food_entry_meal_id']") 
      WebElement addFoodToMeal; 


      public void searchFoodItem() throws Exception{ 

          TextBoxHelper.set(driver,searchFoodDatabase,"Banana"); 
          ButtonHelpers.click(driver, searchFoodDatabaseButton); 

          ButtonHelpers.click(driver, matchedFoodThirdOption); 

          Utils.waitForElement(driver, By.xpath(".//*[@id='loaded_item']"), 2000); 

          foodEntryQuantity.clear(); 
          foodEntryQuantity.sendKeys("2.0"); 

          Select WeightPickerDropDown = new Select(foodEntryWeight); 
          WeightPickerDropDown.selectByIndex(2); 

          Select MealPickerDropDown = new Select(addFoodToMeal); 
          MealPickerDropDown.selectByIndex(2); 

          ButtonHelpers.jsClick(driver, addFoodToDiary); 
          System.out.println("after clicking the add button"); 

      } 
+0

的HTML是无效的。 (如何'p'标签可以在'select'?) –

+0

@MoshFeu该HTML标记来自我试图自动化的网站。 – AdiBoy

+0

我明白..但也许这就是问题所在。我对此并不熟悉,但对我来说脚本会因此而中断,但又不熟悉。 –

回答

0

你JavascriptExecutor代码可能是错的

((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('value', '2.0')", WebElement); 
+0

感谢博客,我会看看它。但是,javscript代码是我认为会起作用的解决方案之一。我使用了自定义点击和其他解决方案,但没有运气。 – AdiBoy

+0

我试着用你分享的东西,但我仍然无法找到元素。 – AdiBoy

+0

当您尝试waitForElementToBeVisible时,您会收到什么消息。请先检查元素是否可见? –