【问题标题】:How to send text to the search field through Selenium Webdriver?如何通过 Selenium Webdriver 向搜索字段发送文本?
【发布时间】:2018-12-26 04:09:09
【问题描述】:

任务: 在搜索框中搜索 FAA:

我试过这个:-

webdriver.select_tabs(search.btnSearch);

Thread.sleep(3000);
WebElement searchbox = driver.findElement(By.id("search-text"));
Actions builder = new Actions(driver);
Actions seriesOfActions = builder.moveToElement(searchbox).click().sendKeys(searchbox, "FAA");
seriesOfActions.perform();

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"search-text\"]")));
element.sendKeys("FAA");
element.sendKeys(Keys.ENTER);

webdriver.enter_key(search.txtSearch, Keys.ENTER);
webdriver.enter_Text(search.txtSearch, "FAA");
webdriver.enter_key(search.txtSearch, Keys.ENTER);

得到这个错误:-

org.openqa.selenium.ElementNotVisibleException: element not visible

【问题讨论】:

  • 请提供HTML源代码。

标签: java selenium selenium-webdriver xpath css-selectors


【解决方案1】:

要将字符序列发送到网站https://faatoday.com/ 上的搜索字段,您需要诱导WebDriverwait 等待搜索图标可点击,然后再次诱导WebDriverWait 再次使所需的元素可点击,然后按如下方式发送字符序列:

  • 代码块:

    driver.get("https://faatoday.com/");
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#navbarNav span.sicon-holder.fabutton#searchicon>i.fa.ssearch.fa-search.fa-lg#sicons"))).click();
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='navbarNav']//input[@class='search-text form-control mr-sm-2' and @id='search-text']"))).sendKeys("FAA");
    
  • 浏览器快照:

【讨论】:

    【解决方案2】:

    在xpath下面使用:

    (//input[@id='search-text'])[2]
    

    并像这样使用:

    driver.findElement(By.xpath("(//input[@id='search-text'])[2]")).sendKeys("FAA");
    

    当你在控制台中通过这个 id 找到它时,它给出了两个元素,第一个是不可见的,但第二个是实际的输入框。

    【讨论】:

      【解决方案3】:

      根据定义,Selenium 与浏览器的交互就像真正的用户一样。真正的用户将无法输入隐藏的文本框/编辑框。您需要更改输入的可见性,重新评估为什么需要与隐藏元素交互,或者使用 javascript 执行器设置输入的值,如下所示:

      driver.executeScript("arguments[0].value='" + textToEnter + "'", element);
      

      【讨论】:

        猜你喜欢
        • 2019-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多