简体   繁体   English

Selenium 自动化:如何找到要发送文本的元素

[英]Selenium Automation: How to find element to send text

I would like to automate a test in Microsoft Redeem Code page我想在 Microsoft Redeem Code 页面中自动执行测试

https://account.microsoft.com/billing/redeem https://account.microsoft.com/billing/redeem

I tried to enter value by below code:我试图通过以下代码输入值:

driver.findElement(By.id("tokenString")).sendKeys("GXXFV-CPMKR-VQVFV-2CCFD-J47GZ");

Inspect element showing:检查元素显示:

<input aria-label="Enter 25-character code" name="tokenString" type="text" autocomplete="off" spellcheck="true" placeholder="Enter 25-character code" maxlength="tokenLength" autofocus="" autocorrect="off" autocapitalize="off" id="tokenString" class="ember-text-field ember-view">

I tried lot of ways nothing worked out, Spent 4 hrs cant figure out please correct my code Environment:我尝试了很多方法都没有解决,花了 4 小时无法弄清楚请更正我的代码环境:

  • Eclipse IDE - Java - Selenium latest Eclipse IDE - Java - Selenium 最新

  • Mac OS - Chrome Mac 操作系统 - Chrome

Correct Code to find element.更正代码以查找元素。

The desired element is a Ember.js enabled element, so to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies :所需的元素是启用了Ember.js的元素,因此要在元素上click() ,您需要为elementToBeClickable()引入WebDriverWait并且您可以使用以下任一定位器策略

  • cssSelector : CSS选择器

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.ember-text-field.ember-view#tokenString[name='tokenString']"))).sendKeys("GXXFV-CPMKR-VQVFV-2CCFD-J47GZ");
  • xpath : xpath :

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='ember-text-field ember-view' and @id='tokenString'][@name='tokenString']"))).sendKeys("GXXFV-CPMKR-VQVFV-2CCFD-J47GZ");

Update更新

Checkout the following line of code:检查以下代码行:

  • cssSelector : CSS选择器

     new WebDriverWait(driver, Duration.ofSeconds(20)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.ember-text-field.ember-view#tokenString[name='tokenString']"))).sendKeys("GXXFV-CPMKR-VQVFV-2CCFD-J47GZ");
  • xpath : xpath :

     new WebDriverWait(driver, Duration.ofSeconds(20)).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='ember-text-field ember-view' and @id='tokenString'][@name='tokenString']"))).sendKeys("GXXFV-CPMKR-VQVFV-2CCFD-J47GZ");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在<span>Selenium中</span>查找具有特定文本的元素 - How to find a element with specific text in <span> with Selenium 如何使用Selenium或Protractor获取HTML中嵌套元素的文本以实现自动化? - How do i get the text of a nested element in HTML for automation using Selenium or Protractor? Selenium 自动化 - 如何定期单击特定按钮直到获得另一个元素的更新文本值? - Selenium Automation - How to click on particular button periodically until getting another element's updated text value? 如何使用硒将文本发送到div元素中的textarea - how to send text to textarea in div element using Selenium 如何在硒中找到这个元素 - How to find this element in Selenium 如何使用Java在Selenium中通过文本查找Web元素? - How to find a Web Element by text in Selenium using Java? 如何使用以Selenium中的文本开头的类名查找元素 - How to find element using classname starting with the text in Selenium 如何获取/查找表中文本值的元素? Selenium Webdriver - How to get/find the element of text value in table? Selenium Webdriver 硒:如何查找带有特定文本的元素而没有包含选项 - Selenium: how to find element with with specific text without contains option 如何在自动化中查找类似于getText()的元素定位器 - How to find locator of element similar as getText() in automation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM