【问题标题】:Unable to locate element a div element in selenium webdriver无法在 selenium webdriver 中找到元素一个 div 元素
【发布时间】:2017-01-10 12:24:36
【问题描述】:

我正在尝试自动化场景,其中用户搜索一个项目,一旦搜索结果出现,他点击服务大小和该特定项目的数量。

但是,当我尝试通过更改值和选择数量大小来操作文本字段时,我经常遇到 element not found 异常,即使在尝试使用该 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");

            }

【问题讨论】:

  • html 无效。 (p 标签如何在select 中?)
  • @MoshFeu HTML 标记来自我正在尝试自动化的网站。
  • 我明白.. 但也许这就是问题所在。我对此并不熟悉,但对我来说脚本会因此而中断是有道理的,但又一次,不熟悉..
  • @AdiBoy,您能否删除所有不必要的代码行,只留下那些实际上可能导致您的问题的行(HTML 也是如此)?
  • @Andersson 我可以删除不必要的行,但我只是想展示我为克服这个问题而采取的其他方法。我会编辑我的问题。谢谢。

标签: javascript java selenium selenium-webdriver


【解决方案1】:

您的 JavascriptExecutor 代码可能有误

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

【讨论】:

  • 感谢您的博客,我会看看它。但是,javscript 代码是我认为可行的解决方案之一。我使用过自定义点击和其他解决方案,但没有运气。
  • 我尝试使用您分享的内容,但我仍然无法找到该元素。
  • 当您尝试 waitForElementToBeVisible 时收到什么消息。首先检查元素是否可见?
  • 我将 isEnabled() 设为 true,将 isDisplayed() 设为 false。我得到的异常是:无效的元素状态:元素当前不可交互且可能无法操作。
  • 抱歉回复晚了。你能分享我可以开发和运行脚本的网址吗?
猜你喜欢
  • 2014-05-29
  • 2015-05-04
  • 2014-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-02
相关资源
最近更新 更多