【问题标题】:How to insert text within the element through Selenium and Java如何通过 Selenium 和 Java 在元素内插入文本
【发布时间】:2019-03-08 06:46:34
【问题描述】:

有人可以提示我在 selenium 中定位元素吗?

我尝试选择的内容在哪里

WebElement  hello =  driver.findElement(By.className("input-box"));

来自 Eclipse 的错误:

Can't find symbol 'GetGraphicsResetStatus'.
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: .input\-box

再次感谢您。

原版html

<div data-v-7d31c51a="" data-v-126e1ccf="" class="input-box"><textarea data-v-7d31c51a="" placeholder="inputhere~" maxlength="500" autofocus="autofocus" class="textarea" style="height: 60px;"></textarea><div data-v-7d31c51a="" class="indicator" style="bottom: -30px; right: 100px;"><span data-v-7d31c51a="" class="">0</span>/<span data-v-7d31c51a="">500</span></div></div>

尝试出错 WebElement hello = driver.findElement(By.className("textarea"));

找不到符号“GetGraphicsResetStatus”。 线程“主”org.openqa.selenium.NoSuchElementException 中的异常:无法定位元素:.textarea 有关此错误的文档,请访问:http://seleniumhq.org/exceptions/no_such_element.html 构建信息:版本:'3.6.0',修订:'6fbf3ec767',时间:'2017-09-27T16:15:26.402Z'

添加我的 jar 文件和我的代码

enter image description here

【问题讨论】:

  • 您是否尝试过等到元素预设?为了设置值,您必须获取 textarea 元素,而不是 div 元素。

标签: java selenium xpath css-selectors webdriverwait


【解决方案1】:

试试 xpath:

 WebElement hello =  driver.findElement(By.xpath("//div[@placeholder='inputhere~']"));

【讨论】:

  • 你能检查一下元素上方是否有 iframe 吗?
  • 如果在检查器中搜索 iframe 则匹配,但如果搜索
  • 谷歌搜索后,不要认为它有 iframe。
【解决方案2】:

元素是动态元素,因此要将字符序列发送到所需元素,您必须诱导WebDriverWait以使元素可点击 em>,您可以使用以下任一解决方案:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.input-box>textarea.textarea[placeholder^='inputhere']"))).sendKeys("JohnMax");
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='input-box']/textarea[@class='textarea' and starts-with(@placeholder, 'inputhere')]"))).sendKeys("JohnMax");
    

【讨论】:

  • @JohnMax 您能否检查 HTML 并向上遍历并查看元素是否在 <iframe> 标记内并相应地更新问题?
  • 如果在检查器中搜索 iframe 则匹配,但如果搜索
  • @JohnMax 请使用包含 iframe 标记的元素的基于文本的 HTML 更新问题。
  • 谷歌搜索后,不要认为它有 iframe。
【解决方案3】:

您认为inputbox 的类名错误。您的类应该是。

WebElement  hello =  driver.findElement(By.className("textarea"));

【讨论】:

  • 您遇到什么错误?可以发帖吗?还要检查是否有任何 iframe 停止识别元素定位器?
  • 使用 iframe 信息更新帖子。再次感谢您
  • @JohnMax,我让你检查你的 html 中的 iframe。你提供的是 CSS 而不是 html。可以复制整个 html 并作为链接共享。谢谢
  • 好的,让我看看怎么做,这是唯一的 iframe 东西。
  • 谷歌搜索后,不要认为它有 iframe。
猜你喜欢
  • 1970-01-01
  • 2015-08-19
  • 1970-01-01
  • 2019-05-30
  • 1970-01-01
  • 2019-01-30
  • 1970-01-01
  • 1970-01-01
  • 2021-05-18
相关资源
最近更新 更多